繁体   English   中英

如何使用可变函数名称调用standardGeneric

[英]How to call standardGeneric with a variable function name

以下代码引发警告:

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参数构造为一个求值函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM