我正在尝试建立一个类联合来促进方法分派。下面的reprex在全局环境中执行时完全符合我的要求,但只要我将这段代码放入包中,最后一行f(new("a"))就抛出一个错误,指出它无法找到继承的方法。
setClass("x", slots = list(slot ="character"))
setClass("y", slots = list(slot ="character"))
setClass("a", slots = list(slot ="character"))
setClass("b", slots = list(slot ="character"))
setClassUnion("xy", c("x", "y"))
setClassUnion("ab", c("a", "b"))
setClassUnion("xyab", c("xy", "ab"))
setGeneric("f", function(object, ...) standardGeneric("f"))
setMethod("f", "xyab", function(object, ...) print("hi!"))
## print's "hi!" as expected
f(new("a"))我遗漏了什么?
为了便于在新的R会话中重现,这将重现该问题:
library(devtools)
fn <- "codefile.R"
writeLines(
c(
"setClass('x', slots = list(slot ='character'))",
"setClass('y', slots = list(slot ='character'))",
"setClass('a', slots = list(slot ='character'))",
"setClass('b', slots = list(slot ='character'))",
"setClassUnion('xy', c('x', 'y'))",
"setClassUnion('ab', c('a', 'b'))",
"setClassUnion('xyab', c('xy', 'ab'))",
"setGeneric('f', function(object, ...) standardGeneric('f'))",
"setMethod('f', 'xyab', function(object, ...) print('hi!'))"
),
con = fn
)
package.skeleton(code_files = "codefile.R")
devtools::load_all("anRpackage")
f(new("a"))发布于 2020-02-27 10:54:04
https://stackoverflow.com/questions/60264786
复制相似问题