Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

environment - What is the difference between parent.frame() and parent.env() in R; how do they differ in call by reference?

It would be helpful if someone can illustrate this with a simple example?

Also, where would it be useful to use parent.frame() instead of parent.env() and vice versa.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

parent.env is the environment in which a closure (e.g., function) is defined. parent.frame is the environment from which the closure was invoked.

f = function() 
     c(f=environment(), defined_in=parent.env(environment()),  
       called_from=parent.frame())
g = function() 
     c(g=environment(), f())

and then

> g()
$g
<environment: 0x14060e8>

$f
<environment: 0x1405f28>

$defined_in
<environment: R_GlobalEnv>

$called_from
<environment: 0x14060e8>

I'm not sure when a mere mortal would ever really want to use them, but the concepts are useful in understanding lexical scope here

> f = function() x
> g = function() { x = 2; f() }
> h = function() { x = 3; function() x }
> x = 1
> f()
[1] 1
> g()
[1] 1
> h()()
[1] 3

or in the enigmatic 'bank account' example in the Introduction to R. The first paragraph of the Details section of ?parent.frame might clarify things.

Environments are pervasive in R, e.g., the search() path is (approximately) environments chained together in a sibling -> parent relationship. One sometimes sees env = new.env(parent=emptyenv()) to circumvent symbol look-up -- normally env[["x"]] would look first in env, and then in env's parent if not found. Likewise, <<- looks for assignment starting in the parent.env. The relatively new reference class implementation in R relies on these ideas to define an instance-specific environment in which symbols (instance fields and methods) can be found.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...