在R中使用callNextMethod()传递参数的问题

时间:2011-08-25 12:55:01

标签: oop r methods argument-passing s4

我的问题:

为什么callNextMethod()没有按预期将参数传递给下一个方法?

情况:

假设我有两个分层类foobarbarfoo的子类),我有一个可以调度的方法foobar两个类(即,有两个类的方法)。

此外,(子)类bar的方法在使用foo进行一些计算后调用callNextMethod()的方法。

两个方法都有相同的附加参数(默认值)应该传递给foo的方法,只有它是相关的。

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()
})

问题描述:
参数未按预期传递,但默认值取自方法定义。具体来说,在第一种方法中,参数是在调用(TRUE)中指定的,但是,它会在下一个方法中更改为FALSE

o1 <- new("bar", x = 4)

foobar(o1, another.argument = TRUE)

给出

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

我希望将another.argument传递给下一个方法,以便在调用TRUE方法时foo


?callNextMethod我得到它应该按预期工作(即,命名参数在调用中传递):

  

对于一个正式的论点,比如x,出现在原始的电话中,那里   是等效于x =的下一个方法调用中的对应参数   X。实际上,这意味着下一个方法看到了相同的实际情况   参数,但参数只评估一次。


我的第二个问题:如何将another.argument传递给下一个方法。 (我真的想在两种方法中都保留默认参数)

1 个答案:

答案 0 :(得分:4)

我认为这与定义了与泛型不同的签名的方法有关(在函数.local中)

> 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" 

解决方法是定义泛型和方法以具有相同的签名

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

或将参数明确传递给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, ...)
})