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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
parent.envis the environment in which a closure (e.g., function) is defined.parent.frameis the environment from which the closure was invoked.and then
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
or in the enigmatic ‘bank account’ example in the Introduction to R. The first paragraph of the Details section of
?parent.framemight clarify things.Environments are pervasive in R, e.g., the
search()path is (approximately) environments chained together in a sibling -> parent relationship. One sometimes seesenv = new.env(parent=emptyenv())to circumvent symbol look-up — normallyenv[["x"]]would look first inenv, and then inenv‘s parent if not found. Likewise,<<-looks for assignment starting in theparent.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.