Skip to content

Active bindings in attached environments

6 messages · Jeffrey Horner, Romain Francois, Tony Plate

#
Hi,

Is this expected behavior for active bindings in attached
environments, or is this a bug:
[1] "x"
[1] ".GlobalEnv"        "e"                 "package:graphics"
[4] "package:grDevices" "package:datasets"  "package:utils"
[7] "package:methods"   "Autoloads"         "package:base"
function() 'foo'

Should this print 'foo' ? The following works as I would expect:
[1] "foo"

but this doesn't:
function() 'foo'

However, changing the environment of f does:
[1] "foo"


Jeff
#
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:

  
    
#
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:

  
    
#
On Thu, Nov 5, 2009 at 9:53 AM, Jeffrey Horner <jeffrey.horner at gmail.com> wrote:
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
#
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: