如何评价R中的其他函数内函数调用的参数 [英] How to evaluate arguments of a function call inside other function in R

查看:122
本文介绍了如何评价R中的其他函数内函数调用的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解如何使用嵌套函数调用和论证评估工作的麻烦。

I'm having trouble of understanding how to work with nested function calls and argument evaluations.

下面是一个简单的例子。我有一个顶级函数 topfunction 一个数字参数。里面的 topfunction 我调用另一个函数 lowerfunction 这说法是里面的 lowerfunction 。

Here's a simple example. I have a top-level function topfunction with one numeric argument. Inside of topfunction I call another function lowerfunction which argument is a call to a function defined inside of lowerfunction.

topfunction<-function(x){  
  lowerfunction(myfun(first=x[1],second=x[2],third=if(length(x)>2) x[3]))
}

lowerfunction<-function(mycall){ 

  myfun<-function(first,second=0,third=NULL){
    print(first)
    print(second)
    print(third)
  }

  mc<-match.call(definition = myfun, call = match.call()[[2]]) 
  eval(mc) 
}

lowerfunction 我捕捉函数调用 match.call ,并尝试评估调用。但作为变量 X 仅在 topfunction 的环境中,评估失败定义为:

Inside of lowerfunction I capture the function call with match.call, and try to evaluate the call. But as variable x is only defined in the environment of topfunction, the evaluation fails:

topfunction(x=1:3)
Error in print(first) : object 'x' not found

我知道,我可以改变行

I know that I could change the line

lowerfunction(myfun(first=x[1],second=x[2],third=if(length(x)>2) x[3]))

lowerfunction(substitute(myfun(first=x[1],second=x[2],third=if(length(x)>2) x[3])))

topfunction ,但在我的实际应用中 topfunction 是由用户构建,因此该解决方案应该发生不知何故在 lowerfunction ,甚至在 myfun 的水平。但是,当他们已经​​失去了约信息X ,我不知道这是否能实现?

in topfunction, but in my real application the topfunction is constructed by the user, so the solution should happen somehow in the lowerfunction or even in the myfun level. But as they have already lost the information about x, I don't know if that can be accomplished?

在实际应用中的 topfunction 构造使用 lowerfunction 模型并计算其可能性,而的参数 lowerfunction 是可以包含函数调用,这将通过评估的公式。这些功能仅在 lowerfunction 定义。此外, lowerfunction ,也可以直接调用,即

In the real application the topfunction constructs the model using lowerfunction and computes its likelihood, whereas the argument of lowerfunction is a formula which can contain function calls, which will be evaluated via eval. These functions are only defined inside the lowerfunction. Also, lowerfunction can also be called directly, i.e.

x<-1:3
lowerfunction(myfun(first=x[1],second=x[2],third=if(length(x)>2) x[3]))
# or
lowerfunction(myfun(first=x1,second=2)

因此​​,这增加解决方案 X 来的参数列表 lowerfunction 不适用一般。

So solutions which add x to the argument list of lowerfunction are not applicable in general.

所以,问题是,评估 myfun 的定义从一个环境(包命名空间中,或在这种情况下,从环境 lowerfunction ),并评估 myfun 的论点,即在环境等环保 topfunction

So the problem is that eval should take the definition of myfun from one environment (package namespace, or in this case from the environment of lowerfunction), and evaluate the arguments of myfun in other environment i.e in the environment of topfunction.

推荐答案

这是一个相对简单的问题,而是因为你正在做的非常的非标准的评价,你需要创建一个新的环境并确保所有你需要的是从环境中访问的所有对象。

This is a relatively straightforward problem, but because you're doing very non-standard evaluation you'll need to create a new environment and all ensure all the objects you need are accessible from that environment.

g <- function(x){  
  f1(f2(x[1], x[2], if(length(x) > 2) x[3]))
}

f1 <- function(mycall, parent = parent.frame()) {
  # Parent contains x
  # New environment that contains f2 and inherits from the parent
  env <- new.env(parent = parent)
  env$f2 <- function(first, second = 0,third = NULL) {
    print(first)
    print(second)
    print(third)
  }

  # More idiomatic way of getting unevaluated expression
  expr <- substitute(mycall)
  eval(expr, env)
}

g(1:3)

我形容我的领域特定语言章类似技术

这篇关于如何评价R中的其他函数内函数调用的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆