Hi all, I am trying to understand the R's "environment" concept however the underlying help files look quite technical to me. Can experts here provide me some more intuitive ideas behind this concept like, why it is there, what exactly it is doing in R's architecture etc.? I mainly need some non-technical intuitive explanation. Thanks,
This **is** an inherently technical topic. Did you try the R Language Manual section on environments (wasn't clear from your message)? You might try posting on R-devel. Folks there may know of tutorials/books that might be useful to you. -- Bert On Mon, Jul 18, 2011 at 11:16 AM, Nipesh Bajaj <bajaj141003 at gmail.com> wrote:> Hi all, I am trying to understand the R's "environment" concept > however the underlying help files look quite technical to me. Can > experts here provide me some more intuitive ideas behind this concept > like, why it is there, what exactly it is doing in R's architecture > etc.? > > I mainly need some non-technical intuitive explanation. > > Thanks, > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- "Men by nature long to get on to the ultimate truths, and will often be impatient with elementary studies or fight shy of them. If it were possible to reach the ultimate truths without the elementary studies usually prefixed to them, these would not be preparatory studies but superfluous diversions." -- Maimonides (1135-1204) Bert Gunter Genentech Nonclinical Biostatistics
*********Initially, I posted this topic in R-help however, folks there suggested me to post this in R-devel forum. Here is my problem********* Hi all, I am trying to understand the R's "environment" concept however the underlying help files look quite technical to me. Can experts here provide me some more intuitive ideas behind this concept like, why it is there, what exactly it is doing in R's architecture etc.? I mainly need some non-technical intuitive explanation. Thanks,
On Mon, Jul 18, 2011 at 7:16 PM, Nipesh Bajaj <bajaj141003 at gmail.com> wrote:> Hi all, I am trying to understand the R's "environment" concept > however the underlying help files look quite technical to me. Can > experts here provide me some more intuitive ideas behind this concept > like, why it is there, what exactly it is doing in R's architecture > etc.? > > I mainly need some non-technical intuitive explanation.You can think of it as a box into which you can put (using "assign") and get (using "get") things. > e=new.env() > assign("foo",99,env=e) > get("foo",env=e) [1] 99 Now, the interesting thing is that when you copy an environment, you dont make a new copy of everything in it: > z=e > get("foo",env=z) [1] 99 Now if I change 'foo' in z, as if by magic it changes in e as well: > assign("foo","Set in z",env=z) > get("foo",env=e) [1] "Set in z" Because e and z are the same environment: > z <environment: 0x8869bec> > e <environment: 0x8869bec> This gives you a way of doing pass-by-reference in R, but you won't be doing it that way soon because R version something has proper reference classes. That's probably all you need to know to get you started. Barry
On 11-07-18 2:16 PM, Nipesh Bajaj wrote:> Hi all, I am trying to understand the R's "environment" concept > however the underlying help files look quite technical to me. Can > experts here provide me some more intuitive ideas behind this concept > like, why it is there, what exactly it is doing in R's architecture > etc.? > > I mainly need some non-technical intuitive explanation. >There are three characteristics that describe environments: 1. They are a collection of named objects. Much of the time when you ask for something by name, you're looking in an "environment" to find it. 2. They have a child-parent relationship to another environment. Some of the time, when you look up a name and it is not found, it goes to the parent to look. (And then the grandparent .... ) This means most of the time when you specify a name, R just looks in one environment and its ancestors to find the object. 3. They don't get copied when you make an assignment. So you can say env <- globalenv(), and your env is another name for the global environment, which is where most user objects are created. Saying env$z <- 3 will create a new variable named z in the global environment. This differs from most other R objects, where assignment makes an independent copy. And one thing that says how they are used: 1. Things like functions need to look up names all the time. Those things generally have an associated environment which is where they'll look. (Functions are a little complicated in that they get a new one every time you call them, but they also have an associated environment which is the parent of the new one.) Duncan Murdoch