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
Duncan Murdoch <murdoch@stats.uwo.ca> writes:> 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? >I'm fairly sure the answer is "nope". It's been annoying me for years, for language aesthetic reasons mostly, but also with some consideration of cases like yours, and I've been on the brink of implementing a version where the base environment was a true environment. Apart from the usual issue of "round tuits", I was held back by the fact that one has to consider at least two things: (a) efficiency. Is it expensive no longer to have the base functions bound directly to their symbol? (My gut feeling is that with suitable hashing and cacheing, the penalty is minimal.) (b) you can *only* use get and simple variable retrieval in a non-base environment with a NULL parent (eval(x <- 1, envir=foo) would give 'couldn't find function "<-"' or so). This could cause some confusion. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907
Seemingly Similar Threads
- A where() functions that does what exists() does but return the environment when object lives?
- Hashes as S4 Classes, or: How to separate environments
- how to control the environment of a formula
- Return function from function with minimal environment
- Rf_defineVar(symbol, R_UnboundValue, environment) questions