R中的动态范围问题 [英] Dynamic scoping questions in R

查看:120
本文介绍了R中的动态范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Hadley的AdvancedR,并正在此 URL

I'm reading the AdvancedR by Hadley and am testing the following code on this URL

subset2 = function(df, condition){
  condition_call = eval(substitute(condition),df )  

  df[condition_call,]
}

df = data.frame(a = 1:10, b = 2:11)

condition = 3

subset2(df, a < condition)

然后我收到以下错误消息:

Then I got the following error message:

eval(substitute(condition),df)中的错误:找不到对象'a'

Error in eval(substitute(condition), df) : object 'a' not found

我阅读了以下说明,但不太了解:

I read the explanation as follows but don't quite understand:

如果eval()在数据框内(第二个参数)找不到该变量,则它将在subset2()的环境中查找.显然这不是我们想要的,因此我们需要某种方法来告诉eval()如果找不到数据框中的变量,该在哪里查看.

If eval() can’t find the variable inside the data frame (its second argument), it looks in the environment of subset2(). That’s obviously not what we want, so we need some way to tell eval() where to look if it can’t find the variables in the data frame.

在我看来,虽然"eval(substitute(condition),df)"找不到的变量是condition,那么为什么找不到对象"a"?

In my opinion, while "eval(substitute(condition),df )", the variable they cannot find is condition, then why object "a" cannot be found?

另一方面,为什么下面的代码不会出错?

On the other hand, why the following code won't make any error?

subset2 = function(df, condition){
  condition_call = eval(substitute(condition),df )  

  df[condition_call,]
}

df = data.frame(a = 1:10, b = 2:11)

y = 3

subset2(df, a < y)

推荐答案

这个更精简的示例可能使您更容易了解Hadley示例中的情况.首先要注意的是,符号condition在这里以四个不同的角色出现,我为每个角色都标记了带编号的注释.

This more stripped down example may make it easier for you to see what's going on in Hadley's example. The first thing to note is that the symbol condition appears here in four different roles, each of which I've marked with a numbered comment.

                              ## Role of symbol `condition`

f <- function(condition) {    #1 -- formal argument
    a <- 100
    condition + a             #2 -- symbol bound to formal argument
}

condition <- 3                #3 -- symbol in global environment

f(condition = condition + a)  #4 -- supplied argument (on RHS)
## Error in f(condition = condition + a) (from #1) : object 'a' not found

要理解的另一重要事项是,在调用函数的求值框架中搜索提供的自变量(此处为#4condition = condition + a的右侧部分)中的符号.来自第4.3.3节,参数评估的R语言定义:

The other important thing to understand is that symbols in supplied arguments (here the right hand side part of condition = condition + a at #4) are searched for in the evaluation frame of the calling function. From Section 4.3.3 Argument Evaluation of the R Language Definition:

关于函数自变量的评估要了解的最重要的事情之一是,对提供的自变量和默认自变量进行区别对待.提供给函数的参数在调用函数的求值框架中求值.函数的默认参数在函数的求值框架中求值.

One of the most important things to know about the evaluation of arguments to a function is that supplied arguments and default arguments are treated differently. The supplied arguments to a function are evaluated in the evaluation frame of the calling function. The default arguments to a function are evaluated in the evaluation frame of the function.

在上面的示例中,对f()的调用的评估框架是全局环境.GlobalEnv.

In the example above, the evaluation frame of the call to f() is the global environment, .GlobalEnv.

逐步执行此操作,这就是调用(condition = condition + a)时发生的情况.在函数求值期间,R在函数体中(在#2处)遇到表达式condition + a.它搜索acondition的值,并找到本地分配的符号a.它发现符号condition已绑定到名为condition的形式参数(在#1处).在函数调用期间提供的 that 形式参数的值是condition + a(在#4).

Taking this step by step, here is what happens when you call (condition = condition + a). During function evaluation, R comes across the expression condition + a in the function body (at #2). It searches for values of a and condition, and finds a locally assigned symbol a. It finds that the symbol condition is bound to the formal argument named condition (at #1). The value of that formal argument, supplied during the function call, is condition + a (at #4).

如R语言定义中所述,在调用函数的环境(此处为全局环境)中搜索表达式condition + a中符号的值.由于全局环境包含一个名为condition的变量(在#3处分配),但没有一个名为a的变量,因此它无法计算表达式condition + a(在#4处),并因错误而失败见.

As noted in the R Language Definition, the values of the symbols in the expression condition + a are searched for in the environment of the calling function, here the global environment. Since the global environment contains a variable named condition (assigned at #3) but no variable named a, it is unable to evaluate the expression condition + a (at #4), and fails with the error that you see.

这篇关于R中的动态范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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