簡體   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