Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用变量函数名调用standardGeneric_R_S4 - Fatal编程技术网

如何使用变量函数名调用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
参数构造为一个求值函数