Hi,
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...
Method definitions can have default expressions for arguments. If
those arguments are then missing in the call to the generic
function, the default expression in the method is used. If the
method definition has no default for the argument, then the
expression (if any) supplied in the definition of the generic
function itself is used. But note that this expression will be
evaluated in the environment defined by the method.
So, I try this...
setGeneric("test",function(x,y){standardGeneric("test")})
setMethod("test","numeric",
function(x,y=FALSE){
browser()
}
)
> test(5)
Called from: test(5)
Browse[1]> x
[1] 5
Browse[1]> y
Error: argument "y" is missing, with no default
Why doesn't it find the default setting of y?
If instead I define the generic as...
setGeneric("test1",function(x,...){standardGeneric("test1")})
setMethod("test1","numeric",
function(x,y=FALSE){
browser()
}
)> test1(5)
Called from: .local(x, ...)
Browse[1]> x
[1] 5
Browse[1]> y
[1] FALSE
In this case I can access the default value of y because it is not an
argument to the generic function. However, this seems to contradict the
help. The help implies that the default does not need to be missing from the
generic. In fact, it states that only if the default value is missing from
the method will the default be retrieved from generic function. If the help
is correct, then why doesn't the code in the for "test" above
work? If the
help is wrong, then how does one set defaults in methods?
Thanks,
Steve
[[alternative HTML version deleted]]