【问题标题】:assign a function to a variable in R将函数分配给R中的变量
【发布时间】:2023-03-22 00:35:01
【问题描述】:

问题在标题中。我想做这样的事情:

myfunc<- pexp
plot(function(x) myfunc(x, 0.5))

我想调用脚本中作为参数给出的几个函数。我想使用 foreach 而不是一堆 if-then-else 语句:

假设我这样调用我的脚本:

R --slave -f script.R --args plnorm pnorm 

我想做这样的事情:

#only get parameters after --args
args<-commandArgs(trailingOnly=TRUE)
for i in args {
    plot(function(x) i(x,param1,param2))
}

【问题讨论】:

    标签: r function variables variable-assignment


    【解决方案1】:

    使用get 检索给定包含其名称的字符串的对象。在这种情况下,您也可以使用getFunction,特定版本来检索函数。

    for f in args {
         f <- getFunction(f)
         plot(function(x) f(x, param1, param2))
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-31
      • 1970-01-01
      • 2012-07-23
      • 2018-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多