[R] assign / environment side effect on R 2.4.0
Thomas Petzoldt
Thomas.Petzoldt at TU-Dresden.de
Fri Jun 23 16:30:46 CEST 2006
Hello,
I got several off-list answers to my question on R-Help:
"[R] list of interdependent functions" from 2006-06-20
and by evaluating this (and also my own example) I found differences in
the behavior of older versions of R (R 2.2.1 and 2.3.1) and the most
recent R 2.4. under development (SVN revision 38399, WinXP SP 2).
The example is a constructed cut-down example, but this behavior is
observed also in different versions of the full implementation.
While in the older versions the environment of L$test remains
R_Globalenv, a changed environment is returned under R 2.4.0
Ist this side effect a "bug" or a "feature"? The list(unlist(L))
workaround helps to avoid the side effect.
Thomas
envfun <- function(L) {
# L <- as.list(unlist(L)) # !!! workaround
p <- parent.frame()
assign("test", L$test, p)
environment(p[["test"]]) <- p
}
solver <- function(L) {
envfun(L)
# some other stuff
}
L <- list(test = function() 1 + 2)
e1 <- environment(L$test)
solver(L)
e2 <- environment(L$test)
print(e1)
# <environment: R_GlobalEnv>
print(e2)
# <environment: 0x01d0b088>
--
Thomas Petzoldt Tel. +49-351-463 3 4954
Technische Universitaet Dresden Fax +49-351-463 3 7108
Institut fuer Hydrobiologie thomas.petzoldt at tu-dresden.de
01062 Dresden http://tu-dresden.de/hydrobiologie/
GERMANY
Upcoming: German Limnology Conference! http://tu-dresden.de/dgl2006
More information about the R-help
mailing list