Skip to content

is.environment, as.environment, and NULL

5 messages · John Chambers, Kurt Hornik, Brian Ripley +1 more

#
The r-devel (1.4) code has C implementations now of the class and
class<- functions in package methods, and also of as.environment.

The last brings up a question (it shows up in the isas-tests test
suite).

as.environment(NULL) is NULL.  Seems like it has to be, since NULL is
the environment associated with package:base.

But is.environment(NULL) is FALSE, which is also fairly natural, if we
think of testing the type of the argument.

The inconsistency causes a related test in isas-tests.R to produce a
FALSE (where generally they should either evaluate to TRUE or generate
an error).

Looking at the uses of is.environment in the R source itself, most of
them seem to hedge their bets on whether is.environment(NULL) is TRUE or
FALSE.

There isn't a completely consistent solution, given the use of NULL as
an environment.  For the moment it seemed best to live with that one
anomalous result in the tests.

John
#
John Chambers <jmc@research.bell-labs.com> writes:
The use of NULL for the base environment is something which I would
like to see go away anyway. It makes little sense semantically (if
anything, you would want the NULL environment to be an empty one), and
there are pitfalls where a NULL value returned by accident can cause
code to work in base but not in a package.

I did start to look into it at some point but ran out of stamina or
got distracted (probably by release timings as usual...). As I recall
it, NULL for base envir is only really used in a handful of places as
a sentinel for searches through the search path, as in:

    for (rho = R_GlobalEnv; rho != R_NilValue; rho = ENCLOS(rho)) ..

The base environment is special because the symbol lookups are
different there, but it could be represented by R_BaseEnv or some such
instead of R_NilValue. I doubt that much interpreted code needs to
explicitly test whether an environment is the base environment. One
thing to watch out for might be saved workspaces, though. On the other
hand, we do not save the search path, so maybe not...
#
A question, and a remark.

Question, would we want to keep as.environment(NULL) to give the base
package, so that code like ls(envir = NULL) will still work?

Remark, we once discussed making package environments (in particular for
base) read-only.  One way of doing this is via setting attributes, but
we cannot do this on NULL.  Not that I think this is the way to do it,
though ...

-k
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Tue, 28 Aug 2001, Kurt Hornik wrote:

            
Interesting.  Was it ever documented to?
NULL is just shorthand: there is a proper R object for base:
<environment: R_GlobalEnv>
[1] FALSE

It was news to me that NULL for .GlobalEnv worked at R level.

Brian
#
Prof Brian Ripley <ripley@stats.ox.ac.uk> writes:
Implicitly at least, but ls("package:base") has been preferable for a
long time.
Nope...
It doesn't. NULL is the *base* environment i.e. the one at the very
end of the search path. This is what gives the mess where things like
evalq(foo(x), NULL) will find "foo" in base, but not in a package.
(One gotcha being that taking environment(formula) returns NULL if no
environment was ever set.)