From hadley at rice.edu  Fri Jul 16 14:32:38 2010
From: hadley at rice.edu (Hadley Wickham)
Date: Fri, 16 Jul 2010 14:32:38 +0200
Subject: [Rd] Creating an environment with attributes in a package
In-Reply-To: <AANLkTin3LINSssOpwJtqsUsW1Mt-1PH8h9eIiW7H4xny@mail.gmail.com>
References: <AANLkTin3LINSssOpwJtqsUsW1Mt-1PH8h9eIiW7H4xny@mail.gmail.com>
Message-ID: <AANLkTinfhXQJCXuUSERSUfyBuncOZZdjLDnVDtPP-28a@mail.gmail.com>

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/



[Rd] Creating an environment with attributes in a package
Hadley Wickham