使用R中第二个函数内一个函数的变量值

ALs*_*ALs 3 parameters r function

这可能是一个非常简单的答案,但似乎无法找到解决方案.我有一个函数,它提供了一组参数:

theta <-
function(
e = 0.2,l= 0.01,p= 0.05)
return(c(e=e,l=l,p=p))
Run Code Online (Sandbox Code Playgroud)

因此,我可以从中返回一组参数,同时更改其中的一个或多个,例如通过使用

theta(e=0.1) #or
theta(l=0.1)
Run Code Online (Sandbox Code Playgroud)

我的问题是我想在另一个函数中调用此函数,其中该函数的输入是变量之一.

所以例如一个函数如:

randFunc<-function(parameter,value){
s<-theta(parameter=value)
return(s)
}
Run Code Online (Sandbox Code Playgroud)

然后用

randFunc("e",0.1) #or
randFunc("l",0.3)
Run Code Online (Sandbox Code Playgroud)

但是我会得到错误"theta中的错误(参数=值):unused argument(parameter = value)"

我尝试了一些东西,但似乎无法获得在theta函数中使用的参数"value".

Joh*_*han 8

另一种方法是使用do.call:

randFunc <- function(parameter, value){
    L = list(value)
    names(L) <- parameter
    do.call(theta, L)
}

> randFunc('e', 0.1)
   e    l    p 
0.10 0.01 0.05 
> randFunc('l', 0.3)
   e    l    p 
0.20 0.30 0.05 
Run Code Online (Sandbox Code Playgroud)