R 如何访问嵌套问题中的函数环境变量?

R 如何访问嵌套问题中的函数环境变量?,r,function,plyr,R,Function,Plyr,我一直在为R中的剂量反应分析开发一些函数,这些函数使用了uniroot.all()函数。假设我有一个函数,例如: library(rootSolve) library(plyr) examplefunction <- function(x){ adply(mtcars, 1, summarize, solution <- uniroot(function (a){ mpg + x + a}, interval = c(-100,100)))

我一直在为R中的剂量反应分析开发一些函数,这些函数使用了
uniroot.all()
函数。假设我有一个函数,例如:

library(rootSolve)
library(plyr)

examplefunction <- function(x){

  adply(mtcars, 1, summarize,
    solution <- uniroot(function (a){
      mpg + x + a},
      interval = c(-100,100)))

}
在我看来,这太草率了。我想做的是允许
uniroot.all()
从函数环境而不是全局环境访问
x

x
用于
adply()
之外的部分函数,而整个函数是使用
optim()
运行的,因此我不能仅在
uniroot.all()
adply()
函数中定义它们

谢谢你的帮助

library(rootSolve)
library(plyr)

examplefunction <- function(x){

x2 <<- x
  adply(mtcars, 1, summarize,
    solution <- uniroot(function (a){
      mpg + x2 + a},
      interval = c(-100,100)))

}