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


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