[R] Understanding R's "Environment" concept
Barry Rowlingson
b.rowlingson at lancaster.ac.uk
Mon Jul 18 21:11:30 CEST 2011
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
More information about the R-help
mailing list