问题描述
为什么callNextMethod()
无法按预期方式将参数传递给下一个方法?
Why is callNextMethod()
not passing arguments as expected to the next method?
说我有两个层次类foo
和bar
(bar
是foo
的子类),为此我有一个方法foobar
可以为这两个类分派(即,对于这两个类都有方法) ).
Say I have two hierarchical classes foo
and bar
(bar
is subclass of foo
) for which I have a method foobar
that can dispatch for both classes (i.e., has methods for both classes).
此外,(子)类bar
的方法在用callNextMethod()
进行一些计算后调用foo
的方法.
Furthermore, the method for the (sub)class bar
calls the method for foo
after some calculations with callNextMethod()
.
这两个方法都具有相同的附加参数(默认情况下),该参数应传递给foo
的方法,只有该参数才是相关的.
Both methods have the same additional argument (with default) that should be passed to the method for foo
, where only it is relevant.
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
.
Problem description:
The arguments are not passed as expected, but the default values are taken from the method definition. Specifically, in the first method the argument is as specified in the call (TRUE
), however, it changes to FALSE
in the next method.
o1 <- new("bar", x = 4)
foobar(o1, another.argument = TRUE)
给予
[1] "in bar-method: TRUE"
[1] "in foo-method: FALSE"
[1] 4
我希望将another.argument
传递给下一个方法,以便在对foo
方法的调用中它也成为TRUE
.
I want the another.argument
to be passed to the next method so that it is TRUE
in the call to the foo
method, too.
从?callNextMethod
中我得到它应该可以按预期工作(即,在调用中按原样传递了命名参数):
From ?callNextMethod
I get that it should work as expected (i.e., the named argument is passed as it is in the call):
我的第二个问题:如何将another.argument传递给下一个方法. (我真的很想在两个方法中都保留默认参数)
My second question: How can I pass another.argument to the next method. (I would really like to keep default arguments in both methods)
推荐答案
我认为这与定义具有不同于一般特征的签名的方法(在函数.local内)的方式有关
I think this has to do with the way a method with a signature different from the generic is defined (within a function .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"
解决方法是将泛型和方法定义为具有相同签名
The work-around is to either define the generic and methods to have the same signature
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, ...)
})
这篇关于在R中使用callNextMethod()传递参数时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!