如何使用变量函数名调用standardGeneric
以下代码引发警告:如何使用变量函数名调用standardGeneric,r,s4,R,S4,以下代码引发警告: test_slot = "testslot" setGeneric(name = test_slot, def = function(object){ standardGeneric(test_slot) }, where = globalenv() ) [1] "testslot" Warning message: In .recursiveCallTest(body,
test_slot = "testslot"
setGeneric(name = test_slot,
def = function(object){
standardGeneric(test_slot)
},
where = globalenv()
)
[1] "testslot"
Warning message:
In .recursiveCallTest(body, fname) :
the body of the generic function for ‘testslot’ calls 'standardGeneric' to dispatch on a different name ("test_slot")!
我本以为这相当于:
setGeneric(name = "testslot",
def = function(object){
standardGeneric("testslot")
},
where = globalenv()
)
发生了什么?这并不能解释原始代码块失败的原因,但解决方法是:
test_slot = "testslot"
setGeneric(name = test_slot,
def = eval(parse(text = paste0("function(object){",
"standardGeneric('", test_slot, "')",
"}")))
},
where = globalenv()
)
也就是说,将整个def
参数构造为一个求值函数