From tplate at acm.org  Thu Nov  5 21:09:46 2009
From: tplate at acm.org (Tony Plate)
Date: Thu, 05 Nov 2009 13:09:46 -0700
Subject: [Rd] Active bindings in attached environments
In-Reply-To: <a8cee7640911051137v62f57aefm768451e0a9c3b486@mail.gmail.com>
References: <a8cee7640911050753g7ad9f5fcsd839866cb664af36@mail.gmail.com>
	<a8cee7640911051137v62f57aefm768451e0a9c3b486@mail.gmail.com>
Message-ID: <4AF3310A.4020702@acm.org>

An explanation of this would be nice in ?makeActiveBinding, e.g.,

NOTE:

When an environment is attach()'ed to the search path, any active 
bindings in it are not preserved as active bindings.  This happens 
because attach() actually adds a new environment to the search path, and 
copies objects into it.  Thus, active bindings are evaluated and their 
values copied into the attached environment.

(Is this explanation correct?)

-- Tony Plate

(PS: Thunderbird wants to spell-correct "makeActiveBinding" to 
"vindictivenesses", which seems an amusingly appropriate allusion to the 
nature of this "gotcha" :-)

Jeffrey Horner wrote:
> On Thu, Nov 5, 2009 at 9:53 AM, Jeffrey Horner <jeffrey.horner at gmail.com> wrote:
>   
>> Hi,
>>
>> Is this expected behavior for active bindings in attached
>> environments, or is this a bug:
>>
>>     
>>> e <- new.env()
>>> makeActiveBinding('x',function() 'foo',e)
>>> ls(e)
>>>       
>> [1] "x"
>>     
>>> attach(e)
>>> search()
>>>       
>> [1] ".GlobalEnv"        "e"                 "package:graphics"
>> [4] "package:grDevices" "package:datasets"  "package:utils"
>> [7] "package:methods"   "Autoloads"         "package:base"
>>     
>>> x
>>>       
>> function() 'foo'
>>
>> Should this print 'foo' ? The following works as I would expect:
>>
>>     
>>> with(e,x)
>>>       
>> [1] "foo"
>>
>> but this doesn't:
>>
>>     
>>> f <- function() x
>>> f()
>>>       
>> function() 'foo'
>>
>> However, changing the environment of f does:
>>
>>     
>>> environment(f) <- e
>>> f()
>>>       
>> [1] "foo"
>>     
>
> Actually, it is my understanding of attach() which is the bug. The
> attach documentation clearly states that a copy of the object is
> attached, not the object itself.
>
> Thanks,
>
> Jeff
> --
> http://biostat.mc.vanderbilt.edu/JeffreyHorner
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>



[Rd] Active bindings in attached environments
Tony Plate