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

Henrik Bengtsson hb at stat.berkeley.edu
Tue Sep 18 17:59:44 CEST 2007


On 9/18/07, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
> Note that attach() has a return value, so you don't need to use
> as.environment() on the name.
>
> Do note more carefully that the attribute you added to env _has_ 'been
> dropped', so there is nothing special about package environments here.

I am sorry about that - mixing up the "foo" outputs.  Yes, it applies
to all environments.
(I have to stop working in the middle of the night or at least send
emails at that time).

/Henrik

>
> You seem to believe that you can (re-)attach an actual environment.
> However (as the help page says), attach() makes a new environment and
> copies the bindings into that environment so the result of attach() is a
> different environment which does not inherit any attributes from the
> argument to attach().  Nor would it be desirable to do so, for the
> attached environment can be altered independently of the argument to
> attach().
>
> If you name enviroments to look like packages when they are not you are
> likely to break quite a lot of things, and yes, that would be expected.
>
> On Mon, 17 Sep 2007, Henrik Bengtsson wrote:
>
> > 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"
>
> And no "foo" attribute.
>
> >
> > # 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
> >
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>
> --
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>



More information about the R-devel mailing list