Suppose all of your S4 methods associated with a specific common function / S4 method use a formal argument, which must have a specific default value. Intuitively, I would formulate such an argument in the definition of S4 generic (as opposed to specifying it in every definition of a method that would seem somewhat redundant to me).
However, I noticed that this way I run into problems, since it seems that the default value of the formal argument is not sent to the methods and thus an error occurs.
Is this not contrary to the idea of ββhaving a combination of general and methods? Why should I again specify the formal argument in each method separately when the default value is always the same? Can I explicitly send default argument values ββin some way?
Below you will find a brief illustration of the behavior.
General function
setGeneric( name="testFoo", signature=c("x", "y"), def=function( x, y, do.both=FALSE, ... ) { standardGeneric("testFoo") } )
Method
setMethod( f="testFoo", signature=signature(x="numeric", y="numeric"), definition=function( x, y ) { if (do.both) { out <- list(x=x, y=y) } else { out <- x } return(out) } )
Error
> testFoo(x=1, y=2) Error in .local(x, y, ...) : object 'do.both' not found
The do.both fixes it
setMethod( f="testFoo", signature=signature(x="numeric", y="numeric"), definition=function( x, y, do.both=FALSE ) { if (do.both) { out <- list(x=x, y=y) } else { out <- x } return(out) } ) > testFoo(x=1, y=2) [1] 1