Jon Clayden
2010-Jul-16 12:08 UTC
[Rd] Creating an environment with attributes in a package
Dear all, I am trying to create an environment object with additional attributes, viz. Foo <- structure(new.env(), name="Foo") Doing this in a standard session works fine: I get the environment with attr(,"name") set as expected. But if the same code appears inside a package source file, I get just the plain environment with no attributes set. Using a non-environment object works as I would expect within the package (i.e. the attributes remain). I've looked through the documentation for reasons for this, and the only thing I've found is the mention in the language definition that "assigning attributes to an environment can lead to surprises". I'm not sure if this is one of the surprises that the author(s) had in mind! Could someone tell me whether this is expected, please? All the best, Jon -- Jonathan D Clayden, PhD Lecturer in Neuroimaging and Biophysics Radiology and Physics Unit UCL Institute of Child Health 30 Guilford Street LONDON WC1N 1EH United Kingdom t | +44 (0)20 7905 2708 f | +44 (0)20 7905 2358 w | www.homepages.ucl.ac.uk/~sejjjd2/ w | www.diffusion-mri.org.uk/people/1
Gabor Grothendieck
2010-Jul-16 12:15 UTC
[Rd] Creating an environment with attributes in a package
On Fri, Jul 16, 2010 at 8:08 AM, Jon Clayden <jon.clayden at gmail.com> wrote:> Dear all, > > I am trying to create an environment object with additional attributes, viz. > > Foo <- structure(new.env(), name="Foo") > > Doing this in a standard session works fine: I get the environment > with attr(,"name") set as expected. But if the same code appears > inside a package source file, I get just the plain environment with no > attributes set. Using a non-environment object works as I would expect > within the package (i.e. the attributes remain). > > I've looked through the documentation for reasons for this, and the > only thing I've found is the mention in the language definition that > "assigning attributes to an environment can lead to surprises". I'm > not sure if this is one of the surprises that the author(s) had in > mind! Could someone tell me whether this is expected, please? > > All the best, > JonI had documented a few problems I found in R here: http://r-proto.googlecode.com and you might want to go over them. The item you mention is discussed in #1 there. This was written some time ago and there may have been relevant changes to R since then so it would be a good idea to double check everything with the current version of R.
Hadley Wickham
2010-Jul-16 12:32 UTC
[Rd] Creating an environment with attributes in a package
On Fri, Jul 16, 2010 at 2:08 PM, Jon Clayden <jon.clayden at gmail.com> wrote:> Dear all, > > I am trying to create an environment object with additional attributes, viz. > > Foo <- structure(new.env(), name="Foo") > > Doing this in a standard session works fine: I get the environment > with attr(,"name") set as expected. But if the same code appears > inside a package source file, I get just the plain environment with no > attributes set. Using a non-environment object works as I would expect > within the package (i.e. the attributes remain). > > I've looked through the documentation for reasons for this, and the > only thing I've found is the mention in the language definition that > "assigning attributes to an environment can lead to surprises". I'm > not sure if this is one of the surprises that the author(s) had in > mind! Could someone tell me whether this is expected, please?You'll be much less surprised if you do: Foo <- structure(list(new.env()), name="Foo") Attributes on reference objects are also passed by reference, and surprises will result. Hadley -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/
Reasonably Related Threads
- readBin() arg check has unnecessary overhead (patch included)
- R --interactive and readline() creates infinite loop
- Significant updates to medical imaging packages: TractoR and RNiftyReg
- Significant updates to medical imaging packages: TractoR and RNiftyReg
- Capturing all warnings (with messages)