[Rd] Environment with no parent?

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Wed 09 Feb 2005 - 06:07:34 EST


Is it possible to create an environment that has no parent (or an empty parent)? I would have thought

 e <- new.env(parent=NULL)

would work, but it acts as though the parent is the base namespace:

> get("close", envir = e)

function (con, ...)
UseMethod("close")
<environment: namespace:base>

I can use inherits = FALSE in this case:

> get("close", envir = e, inherits = F)
Error in get(x, envir, mode, inherits) : variable "close" was not found

but what I want to do is to create my own hierarchy of environments that allow inheritance from their parents, but which stop when they get to e, and don't continue on into base. For example

> assign('x', 1, envir = e)
>
> f <- new.env(parent = e)
> assign('y', 2, envir = f)
>
># The first two of these work as desired, but I'd like a "not found" error from the last:
>
> get('y', envir=f)

[1] 2
> get('x', envir=f)

[1] 1
> get('close', envir=f)

function (con, ...)
UseMethod("close")
<environment: namespace:base>

Looking in envir.c, I see this:

    /* env is now R_NilValue, the base environment */

which doesn't give me much hope, but maybe there's a trick....

If not, would it be reasonable to install a magic "EmptyEnv" to use as a parent in this sort of situation?

Duncan Murdoch



R-devel@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed Feb 09 05:36:25 2005

This archive was generated by hypermail 2.1.8 : Fri 18 Mar 2005 - 09:02:46 EST