R语言 带有附加参数的 S4 类 [(子集)继承



这是在R中的访问器函数中使用callNextMethod()的扩展。

2017-03-25 更新:为了说明这仅在加载方法时失败,而不是在构建的包中失败,我创建了一个虚拟包:https://github.com/zkamvar/inheritest#readme

基本问题:

我有一个继承另一个类 foo 的类栏,它们都有 [ 方法的附加参数。foo的方法始终有效,但bar的方法在第一次使用后失败。

错误和回溯:

Error in callNextMethod(x, i, j, ..., drop): bad object found as method (class "function")
4: stop(gettextf("bad object found as method (class %s)",  dQuote(class(method))), 
domain = NA)
3: callNextMethod(x, i, j, ..., drop) at #9
2: .local(x, i, j, ..., drop = drop)
1: BAR["x"]

更多详情:

我有一个包,它实现了一个依赖于另一个包中的类的类。构建包后,一切正常,但是当我的包被简单地加载(使用devtools::load_all("."))时,我得到以下行为。

最小工作示例:


foo <- setClass("foo", representation(x = "numeric", y = "numeric"))
bar <- setClass("bar", representation(distance = "numeric"), contains = "foo")
setMethod(f = "[", signature = signature(x = "foo", i = "ANY", j = "ANY", drop = "ANY"), 
definition = function(x, i, j, ..., foo = TRUE, drop = FALSE) {
if (foo) 
message("FOOOOOOO")
if (i == "x") {
return(x@x)
} else {
if (i == "y") {
return(x@y)
}
}
})
#> [1] "["
setMethod(f = "[", signature = signature(x = "bar", i = "ANY", j = "ANY", drop = "ANY"), 
definition = function(x, i, j, ..., bar = TRUE, drop = FALSE) {
if (bar) 
message("BAAAAAAR")
if (i == "distance") {
return(x@distance)
} else {
callNextMethod(x, i, j, ..., drop)
}
})
#> [1] "["
FOO <- new("foo", x = 1, y = 4)
BAR <- new("bar", x = 1, y = 4, distance = 3)
FOO["x"]
#> FOOOOOOO
#> [1] 1
BAR["x"]
#> BAAAAAAR
#> FOOOOOOO
#> [1] 1
FOO["x"]
#> FOOOOOOO
#> [1] 1
BAR["distance"]
#> BAAAAAAR
#> [1] 3
BAR["x"]  # fails
#> BAAAAAAR
#> Error in callNextMethod(x, i, j, ..., drop): bad object found as method (class "function")
BAR["x", foo = FALSE]
#> BAAAAAAR
#> [1] 1

注意:当我通过 reprex 传递它时,对 BAR 的第一次和最后一次调用也导致了错误,但我正在展示我在交互式会话中的体验。我正在使用 R 版本 3.3.3

这是因为callNextMethod()不够聪明,无法处理具有增强形式形式的基元方法。我已经修复了它,并将很快提交到后备箱。

这里有一个部分答案:它与"["有关。下面是一些工作代码,它将"["方法替换为"bat"方法。它对我来说工作正常:

foo <- setClass("foo", representation(x = "numeric", y = "numeric"))
bar <- setClass("bar", representation(distance = "numeric"), contains = "foo")
bat <- function (x, i, j, ..., drop = FALSE) message('in bat')
setGeneric('bat')
setMethod(f = "bat", signature = signature(x = "foo"), 
definition = function(x, i, j, ..., foo = TRUE, drop = FALSE) {
if (foo) 
message("FOOOOOOO")
if (i == "x") {
return(x@x)
} else {
if (i == "y") {
return(x@y)
}
}
})
#> [1] "["
setMethod(f = "bat", signature = signature(x = "bar"), 
definition = function(x, i, j, ..., bar = TRUE, drop = FALSE) {
if (bar) 
message("BAAAAAAR")
if (i == "distance") {
return(x@distance)
} else {
callNextMethod(x, i, j, ..., drop)
}
})
FOO <- new("foo", x = 1, y = 4)
BAR <- new("bar", x = 1, y = 4, distance = 3)
bat(FOO, 'x')
bat(BAR, 'distance')
bat(BAR, 'x')

现在:

bat(FOO, 'x')
FOOOOOOO
[1] 1
bat(BAR, 'x')
BAAAAAAR
FOOOOOOO
[1] 1
bat(BAR, 'distance')
BAAAAAAR
[1] 3
bat(BAR, 'x')
BAAAAAAR
FOOOOOOO
[1] 1

所以,我认为这与 S4 调度和 [自己的调度...和解决方案?我没有,除了像瘟疫一样避免 S4。也许R-devel可以提供帮助。这可能是一个真正的 R 错误,因为代码只会中断[.

这个问题可能与[是原语有关,并且在使用S4时对原语的处理方式不同。深入研究callNextMethod表明,如果该方法与该基元函数的泛型具有不同的参数,则无法正确分析调用堆栈。如果从方法定义中删除参数bar,则调度将正常工作。

也就是说,还有另一种解决方法不需要您选择其他函数名称。我添加了一个额外的函数as.foo并在转换为 foo 对象后调用泛型:

setGeneric("as.foo", function(x)standardGeneric("as.foo"))
setMethod("as.foo", signature = "bar",
function(x)
new("foo", x = x@x, y = x@y))
setMethod(f = "[", signature = signature(x = "bar", i = "ANY", j = "ANY", drop = "ANY"), 
definition = function(x, i, j, ..., bar = TRUE, drop = FALSE) {
if (bar) 
message("BAAAAAAR")
if (i == "distance") {
return(x@distance)
} else {
x <- as.foo(x)
callGeneric()
}
}
)

通过这种方式,您可以绕过调度中的打嗝,并且过去失败的所有代码现在都可以正常工作

FOO["x"]
#> FOOOOOOO
#> [1] 1
BAR["x"]
#> BAAAAAAR
#> FOOOOOOO
#> [1] 1
BAR["distance"]
#> BAAAAAAR
#> [1] 3
BAR["x"]  
#> BAAAAAAR
#> FOOOOOOO
#> [1] 1
BAR["x", foo = FALSE]
#> BAAAAAAR
#> [1] 1

最新更新