Problems passing arguments with calling NextMethod () in R

My question is:

Why doesn't callNextMethod() pass arguments as expected to the next method?

Situation:

Let's say I have two hierarchical classes foo and bar ( bar is a subclass of foo ), for which I have a foobar method that can send for both classes (i.e. has methods for both classes).

In addition, the method for the (sub) class bar calls the method for foo after some calculations using callNextMethod() .

Both methods have the same optional argument (default), which should be passed to the method for foo , where it matters only.

 setClass("foo", representation(x = "numeric")) setClass("bar", contains = "foo") setGeneric("foobar", function(object, ...) standardGeneric("foobar")) setMethod("foobar", "foo", function(object, another.argument = FALSE, ...) { print(paste("in foo-method:", another.argument)) if (another.argument) object@x ^3 else object@x ^2 }) setMethod("foobar", "bar", function(object, another.argument = FALSE, ...) { print(paste("in bar-method:", another.argument)) object@x <- sqrt( object@x ) callNextMethod() }) 

Description of the problem:
Arguments are not passed as expected, but the default values ​​are taken from the method definition. In particular, in the first method, the argument is specified in the call ( TRUE ), however in the next method it changes to FALSE .

 o1 <- new("bar", x = 4) foobar(o1, another.argument = TRUE) 

gives

 [1] "in bar-method: TRUE" [1] "in foo-method: FALSE" [1] 4 

I want another.argument be passed to the next method so that it is TRUE in the foo method call.


From ?callNextMethod I get that it should work as expected (i.e. the named argument is passed as it is in the call):

For a formal argument, say, x, which appears in the initial call, is the corresponding argument in the next method call, equivalent to x = X. Essentially, this means that the next method sees the same arguments, but the arguments are evaluated only once.


My second question . How to pass another method to the next method. (I would really like to keep the default arguments in both methods)

+7
source share
1 answer

I think this is due to how the method is defined with a signature different from the general one (inside the .local function)

 > selectMethod(foobar, "bar") Method Definition: function (object, ...) { .local <- function (object, another.argument = FALSE, ...) { print(paste("in bar-method:", another.argument)) object@x <- sqrt( object@x ) callNextMethod() } .local(object, ...) } Signatures: object target "bar" defined "bar" 

The workaround is to define a common type and methods in order to have the same signature

 setGeneric("foobar", function(object, another.argument=FALSE, ...) standardGeneric("foobar"), signature="object") 

or explicitly pass arguments to callNextMethod

 setMethod("foobar", "bar", function(object, another.argument = FALSE, ...) { print(paste("in bar-method:", another.argument)) object@x <- sqrt( object@x ) callNextMethod(object, another.argument, ...) }) 
+4
source

All Articles