范围 - 如果存在具有相同名称的本地var,如何评估父环境中的对象?

时间:2022-06-01 12:40:06

If I have two nested environments using the same variable name, how would I grab the value of the outer environment?

如果我有两个使用相同变量名的嵌套环境,我如何获取外部环境的值?

I am having difficulty figuring out the correct way to implement this. I have tried a few variations of parent.frame(x) and sys.frame(x) inside eval. Any suggestions would be appreciated.

我很难找出实现这个的正确方法。我在eval中尝试了一些parent.frame(x)和sys.frame(x)的变体。任何建议,将不胜感激。


EXAMPLE:

例:

outerFunc <- function() { 
  obj <- "Outer Object"
  innerFunc()
}

innerFunc <- function() {
  # A local variable with same name is created
  obj <- "Inner Object"

  # would like to grab the value of obj from the outer environment
  obj.callingFunc <- eval(obj, envir=sys.frame(-1))

  cat(obj.callingFunc)  # gives "Inner Object" instead of "Outer Object"
} 

> outerFunc()
Inner Object

The obvious solutions (Pass obj explicitly. Use a different variable name, etc) are not available to me in this specific case.

在这种特定情况下,我无法使用明显的解决方案(显式传递obj。使用不同的变量名称等)。


EDIT

examining @GregSnow's and @Dwin's answers below

在下面查看@ GregSnow和@Dwin的答案

  # THESE WORK
  eval(quote(obj), envir=parent.frame()) # equivalent to evalq(obj, envir=parent.frame())
  get("obj", envir=parent.frame())

  # THESE DO *NOT* WORK
  eval("obj", envir=parent.frame()) 
  get(quote(obj), envir=parent.frame())

Any thoughts as to the effects of quoting in get() vs eval()?

有关get()vs eval()中引用效果的任何想法吗?



2 个解决方案

#1


10  

outerFunc <- function() { 
  obj <- "Outer Object"
  innerFunc()
}

innerFunc <- function() {
  # A local variable with same name is created
  obj <- "Inner Object"

  # would like to grab the value of obj from the outer environment
  cat( get('obj', envir=parent.frame()) )

   } 

 outerFunc()
#Outer Object

Could have also used: eval(quote(obj), envir=sys.frame(-1)). It becomes clear that the meaning of quote(expr) is different than than of "expr". The get function is "expecting" (or perhaps more accurately is designed) to receive character values, whereas eval is expecting calls and expressions, i.e "language" objects. There is sort of a semi-permeable membrane between data and language in R and get is one of the membrane channels through which you can pass character valued objects to language domain.

也可以使用:eval(quote(obj),envir = sys.frame(-1))。很明显,quote(expr)的含义与“expr”的含义不同。 get函数是“期望”(或者更准确地说是设计)来接收字符值,而eval期待调用和表达式,即“语言”对象。 R中的数据和语言之间有一种半渗透膜,get是可以将字符值对象传递到语言域的膜通道之一。

#2


2  

Change your eval to evalq or otherwise quote obj in the call to eval. As is obj is being evaluated before it is given to eval so it has the inner value, quoting it says pass the expression (not the value) to eval to be evaluated.

将您的eval更改为evalq,或者在eval调用中引用obj。因为obj在被赋予eval之前被评估所以它具有内部值,引用它表示将表达式(而不是值)传递给要评估的eval。

#1


10  

outerFunc <- function() { 
  obj <- "Outer Object"
  innerFunc()
}

innerFunc <- function() {
  # A local variable with same name is created
  obj <- "Inner Object"

  # would like to grab the value of obj from the outer environment
  cat( get('obj', envir=parent.frame()) )

   } 

 outerFunc()
#Outer Object

Could have also used: eval(quote(obj), envir=sys.frame(-1)). It becomes clear that the meaning of quote(expr) is different than than of "expr". The get function is "expecting" (or perhaps more accurately is designed) to receive character values, whereas eval is expecting calls and expressions, i.e "language" objects. There is sort of a semi-permeable membrane between data and language in R and get is one of the membrane channels through which you can pass character valued objects to language domain.

也可以使用:eval(quote(obj),envir = sys.frame(-1))。很明显,quote(expr)的含义与“expr”的含义不同。 get函数是“期望”(或者更准确地说是设计)来接收字符值,而eval期待调用和表达式,即“语言”对象。 R中的数据和语言之间有一种半渗透膜,get是可以将字符值对象传递到语言域的膜通道之一。

#2


2  

Change your eval to evalq or otherwise quote obj in the call to eval. As is obj is being evaluated before it is given to eval so it has the inner value, quoting it says pass the expression (not the value) to eval to be evaluated.

将您的eval更改为evalq,或者在eval调用中引用obj。因为obj在被赋予eval之前被评估所以它具有内部值,引用它表示将表达式(而不是值)传递给要评估的eval。