From hpages at fhcrc.org  Thu Jun 14 21:41:13 2007
From: hpages at fhcrc.org (Herve Pages)
Date: Thu, 14 Jun 2007 12:41:13 -0700
Subject: [Rd] Missing args with a default value in S4 methods
Message-ID: <467199D9.6090205@fhcrc.org>

Hi,


Strange things happen with missing args in S4 methods:

  > setGeneric("mygen", signature="x", function(x, ...) standardGeneric("mygen"))
  [1] "mygen"

  > setMethod("mygen", "character", function(x, y=12, z, ...) {cat(missing(y), "\n"); cat(y, "\n")})
  [1] "mygen"

  > mygen("aa", z=99)
  TRUE
  12

  > mygen("aa", , 99)
  TRUE
  Error in cat(y, "\n") : argument is missing, with no default
                                      ^^^^^^^       ^^^^^^^^^^
                                       TRUE          NOT TRUE!


For "normal" functions, things work as expected:

  > myfun <- function(x, y=12, z, ...) {cat(missing(y), "\n"); cat(y, "\n")}

  > myfun("aa", z=99)
  TRUE
  12

  > myfun("aa", , 99)
  TRUE
  12

And with S3 generics too:

  > dd <- data.frame(aa=letters[1:9], ii=9:1)
  > head(dd, z="ignored")
    aa ii
  1  a  9
  2  b  8
  3  c  7
  4  d  6
  5  e  5
  6  f  4

  > head(dd, , "ignored")
    aa ii
  1  a  9
  2  b  8
  3  c  7
  4  d  6
  5  e  5
  6  f  4

Cheers,
H.



[Rd] Missing args with a default value in S4 methods
Herve Pages