简体   繁体   English

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

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

The following code throws a warning: 以下代码引发警告:

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")!

I would have expected that it would have been equivalent to: 我希望它等同于:

setGeneric(name = "testslot", 
           def = function(object){
               standardGeneric("testslot")
           },
           where = globalenv()
)

What happened? 发生了什么?

This does not answer why the original block of code failed, but a workaround would be to do: 这不能回答为什么原始代码块失败的原因,但是可以采用以下解决方法:

test_slot = "testslot"

setGeneric(name = test_slot, 
           def = eval(parse(text = paste0("function(object){",

           "standardGeneric('", test_slot, "')",
       "}")))
           },
           where = globalenv()
)

that is, construct the entire def argument to be an evaluated function. 也就是说,将整个def参数构造为一个求值函数。

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

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