[Rd] Re-attaching a package environment drops the attributes

Henrik Bengtsson hb at stat.berkeley.edu
Tue Sep 18 06:32:31 CEST 2007


Hi.

contrary to other environments, the attributes of a *package*
environment are dropped (from the new environment) when attach():ing
it to the search() path.  This might or might not be surprising, but
have some side effects if rearranging/attaching package environments.

# Example - Regular environments
env <- new.env()
attr(env, "foo") <- "bar"
print(env)
## <environment: 0x023f3128>
## attr(,"foo")
## [1] "bar"
attach(env, pos=2, name="foo", warn.conflicts=FALSE)
env2 <- as.environment("foo")
print(env2)
## <environment: 0x023f44c4>
## attr(,"name")
## [1] "foo"

# Example - Package environments
env <- as.environment("package:utils")
attr(env, "foo") <- "bar"
print(env)
## <environment: package:utils>
## attr(,"name")
## [1] "package:utils"
## attr(,"path")
## [1] "C:/PROGRA~1/R/R-2.6.0alpha/library/utils"
## attr(,"foo")
## [1] "bar"
attach(env, pos=2, name="package:utils2", warn.conflicts=FALSE)
env2 <- as.environment("package:utils2")
print(env2)
## <environment: package:utils2>
## attr(,"name")
## [1] "package:utils2"

This becomes a problem, because the 'path' attribute is dropped causing:

packageDescription("utils2")
## Error in if (pkgpath == "") { : argument is of length zero
sessionInfo()
## Error in if (pkgpath == "") { : argument is of length zero

Again, this might or might not expected.

Finally:

> sessionInfo()
R version 2.6.0 alpha (2007-09-14 r42843)
i386-pc-mingw32

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
States.1252;LC_MONETARY=English_United
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

Cheers

Henrik



More information about the R-devel mailing list