Re: [Rd] Missing args with a default value in S4 methods

From: John Chambers <jmc_at_r-project.org>
Date: Fri, 15 Jun 2007 09:12:32 -0400

This has essentially nothing to do with methods, but rather with the treatment of missing arguments.

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

This is the same argument-matching as your example, since the generic and method have different formal arguments. And indeed,

 > foo("a",,z=99)
TRUE
Error in cat(y, "\n") : argument is missing, with no default

The error message is correct, but the argument in question is not "y" but "..1". This is constructed and passed down as a special R object representing "missing-argument-with-no-default". (Splus would have worked as you expected, because missingness there is a property of the function call, not of the object corresponding to the formal argument.)

Herve Pages wrote:
> 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.
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Fri 15 Jun 2007 - 13:15:49 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 16 Jun 2007 - 10:34:56 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.