【发布时间】:2015-07-13 19:48:52
【问题描述】:
我正在尝试将特定参数动态传递给函数,其中函数具有大多数或所有参数的默认值。
这是一个玩具示例:
library(data.table)
mydat <- data.table(evildeeds=rep(c("All","Lots","Some","None"),4),
capitalsins=rep(c("All", "Kinda","Not_really", "Virginal"),
each = 4),
hellprobability=seq(1, 0, length.out = 16))
hellraiser <- function(arg1 = "All", arg2= "All "){
mydat[(evildeeds %in% arg1) & (capitalsins %in% arg2), hellprobability]}
hellraiser()
hellraiser(arg1 = "Some")
whicharg = "arg1"
whichval = "Some"
#Could not get this to work:
hellraiser(eval(paste0(whicharg, '=', whichval)))
我想要一种动态指定我正在调用的参数的方法:换句话说,获得与hellraiser(arg1="Some") 相同的结果,但同时选择是否动态发送 arg1 OR arg2。目标是能够调用只指定一个参数的函数,并动态指定它。
【问题讨论】:
-
是的,我知道我可以给所有 args 默认值,然后在其中一个上切换一些自定义值,然后执行函数调用,但我想一次性完成。
-
PS:我猜这是一些do.call的事情......?以前从未使用过它,所以我可以使用一些帮助...
标签: r data.table