[R] setting argument defaults in setMethod

Seth Falcon sfalcon at fhcrc.org
Wed Mar 22 18:15:38 CET 2006


"Steven Lacey" <slacey at umich.edu> writes:
> I want to set a default value in a method of a generic function. This seems
> as though it should be possible. From R help on setMethod...
> So, I try this...
>  
> setGeneric("test",function(x,y){standardGeneric("test")})
> setMethod("test","numeric",
>     function(x,y=FALSE){
>         browser()
>     }
> )
>  

I think you have to actually specify a default value in the definition
of the generic (I don't claim this makes any sense).  That value won't
get used as long as you specify a default in the method.

setGeneric("foo", function(x, y=1) standardGeneric("foo"))

setMethod("foo", signature(x="character"), 
          function(x, y="world") cat(x, y, "\n"))

foo("hello")

setMethod("foo", signature(x="character"), 
          function(x, y) cat(x, y, "\n"))

foo("hello")

+ seth




More information about the R-help mailing list