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

Hen*_*rik 7 oop methods r argument-passing s4

我的问题:

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

情况:

假设我有两个分层类foobar(bar是它的子类foo),我有一个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()
})
Run Code Online (Sandbox Code Playgroud)

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

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

foobar(o1, another.argument = TRUE)
Run Code Online (Sandbox Code Playgroud)

[1] "in bar-method: TRUE"
[1] "in foo-method: FALSE"
[1] 4
Run Code Online (Sandbox Code Playgroud)

我希望将another.argument其传递给下一个方法,以便它也可以TRUE调用该foo方法.


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

对于出现在原始调用中的正式参数,例如x,在下一个等效于x = x的方法调用中存在相应的参数.实际上,这意味着下一个方法看到相同的实际参数,但参数只被评估一次.


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

Mar*_*gan 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" 
Run Code Online (Sandbox Code Playgroud)

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

setGeneric("foobar",
    function(object, another.argument=FALSE, ...) standardGeneric("foobar"),
    signature="object")
Run Code Online (Sandbox Code Playgroud)

或将参数显式传递给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, ...)
})
Run Code Online (Sandbox Code Playgroud)