Re: [R] environment question

From: Jeffrey Horner <jeff.horner_at_vanderbilt.edu>
Date: Mon, 28 Jul 2008 21:42:55 -0500

Edna Bell wrote:
> Hi R users!
>
> I was looking at some of the example code for the "environment"
> function. Here it is:
>
> e1 <- new.env(parent = baseenv()) # this one has enclosure package:base.
> e2 <- new.env(parent = e1)
> assign("a", 3, envir=e1)
> ls(e1)
> ls(e2)
> exists("a", envir=e2) # this succeeds by inheritance
> exists("a", envir=e2, inherits = FALSE)
> exists("+", envir=e2) # this succeeds by inheritance
>
> My question is: how can "a" exist in e2 when the ls(e2) gives
> character(0), please?

It actually doesn't. From the code above here's the inheritance tree for the environments:

baseenv() => e1 => e2

When you call exists() on the e2 environment, it actually fails. However since the inherits flag is TRUE by default, exists() searches through the inherited environments and finds "a" in e1. So, exists will tell you that it found "a", just not where it found "a".

However, if you set inherits=FALSE, then exists() searches only in the specified environment.

HTH Jeff

-- 
http://biostat.mc.vanderbilt.edu/JeffreyHorner

______________________________________________
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.
Received on Tue 29 Jul 2008 - 02:43:55 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 Jul 2008 - 03:32:43 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive