do.call指定函数内的环境

bap*_*ste 6 r do.call

我在包中使用以下构造,

## two functions in the global environment
funa <- function(x) x^2
funb <- function(x) x^3
## called within a function, fine
fun_wrap <- function(){
  lapply(c('funa', 'funb'), do.call, list(x=3))
}

fun_wrap()
[[1]]
[1] 9

[[2]]
[1] 27
Run Code Online (Sandbox Code Playgroud)

但是我只是被这样的事实所困扰:如果函数在不同的(本地)框架中它将无法工作,

## same construct, but the functions are local
fun_wrap1 <- function(){
  funa1 <- function(x) x^2
  funb1 <- function(x) x^3
  lapply(c('funa1', 'funb1'), do.call, list(x=3))
}
## now it fails
fun_wrap1()
##Error in FUN(c("funa1", "funb1")[[1L]], ...) : 
##  could not find function "funa1"
Run Code Online (Sandbox Code Playgroud)

我试图传递envir=parent.frame(2)do.call()(不工作); 坦率地说,帮助页面?parent.frame超越了我的脑海.是否有更强大的do.call使用提示?

请注意,函数列表是从另一段代码传递的字符向量; 我不想直接传递函数.

编辑:再扭曲一下......我以为我用我的玩具示例说明了正确的问题,但我使用的实际代码略有不同,从某种意义上说,我fun_wrap1在一个单独的函数中调用.所提出的解决方案在此背景下失败.

fun_wrap1 <- function(funs){
  lapply(funs, do.call, args=list(x=3), envir=environment())
}

foo <- function(){
  funa1 <- function(x) x^2
  funb1 <- function(x) x^3
 fun_wrap1(c('funa1', 'funb1'))
}

foo()
##Error in FUN(c("funa1", "funb1")[[1L]], ...) : 
##  could not find function "funa1"
Run Code Online (Sandbox Code Playgroud)

(这种match.fun方法也是如此)

通过传递可选环境fun_wrap1,我可以让它工作,

fun_wrap1 <- function(funs, e=parent.frame()){
  lapply(funs, do.call, args=list(x=3), envir=e)
}

foo <- function(){
  funa1 <- function(x) x^2
  funb1 <- function(x) x^3
  fun_wrap1(c('funa1', 'funb1'))
}

foo()
Run Code Online (Sandbox Code Playgroud)

这是有希望的.

the*_*ail 5

这似乎有效,但我不确定它是否有其他含义我不考虑:

fun_wrap1 <- function(){
  funa1 <- function(x) x^2
  funb1 <- function(x) x^3
  lapply(c('funa1', 'funb1'), do.call, args=list(x=3), envir=environment())
}

fun_wrap1()
#[[1]]
#[1] 9
#
#[[2]]
#[1] 27
Run Code Online (Sandbox Code Playgroud)

所以这基本上等同于lapply声明如下:

lapply(
       c('funa1', 'funb1'), 
       function(f) do.call(f, args=list(x=3), envir=environment() )
      ) 
Run Code Online (Sandbox Code Playgroud)