[Rd] Active bindings in attached environments
Romain Francois
romain.francois at dbmail.com
Thu Nov 5 19:02:03 CET 2009
Also, active bindings on the search path seem to do only half of the job:
> e <- new.env()
> attach( e )
> makeActiveBinding( "x", function(val) if(missing(val)) "get" else
"set", as.environment(2) )
> x
[1] "get"
> x <- 3
> x
[1] 3
Romain
On 11/05/2009 05:54 PM, Romain Francois wrote:
>
>
> I think it is related to the call to duplicate in do_attach:
>
> defineVar(TAG(p), duplicate(CAR(p)), s);
>
> You can circumvent it by using "importIntoEnv", as in:
>
> > e
> <environment: 0x9070064>
> > e <- new.env()
> > makeActiveBinding('x',function() 'foo',e)
> > f <- new.env()
> > attach( f, pos = 2 )
> > .Internal(importIntoEnv( as.environment(2), ls(e), e, ls(e) ))
> NULL
> > x
> [1] "foo"
>
> Romain
>
> On 11/05/2009 04:53 PM, Jeffrey Horner 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"
>>
>>
>> Jeff
>
--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/BcPw : celebrating R commit #50000
|- http://tr.im/ztCu : RGG #158:161: examples of package IDPmisc
`- http://tr.im/yw8E : New R package : sos
More information about the R-devel
mailing list