From romain.francois at dbmail.com  Thu Nov  5 19:02:03 2009
From: romain.francois at dbmail.com (Romain Francois)
Date: Thu, 05 Nov 2009 19:02:03 +0100
Subject: [Rd] Active bindings in attached environments
In-Reply-To: <4AF30354.4090801@dbmail.com>
References: <a8cee7640911050753g7ad9f5fcsd839866cb664af36@mail.gmail.com>
	<4AF30354.4090801@dbmail.com>
Message-ID: <4AF3131B.8050309@dbmail.com>

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



[Rd] Active bindings in attached environments
Romain Francois