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

时间:2015-05-24 03:11:35

标签: r s4

以下代码会发出警告:

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()
)

发生了什么事?

1 个答案:

答案 0 :(得分:0)

这不能回答原始代码块失败的原因,但解决方法是:

test_slot = "testslot"

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

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

即构造整个def参数作为评估函数。