[R] Question on closure (lexical scoping) and encapsulation

Duncan Murdoch murdoch at stats.uwo.ca
Wed Jun 14 19:10:17 CEST 2000


I was playing with Peter's suggestion in 1.0.1 and came up with
something that might be a bug.  Opinions?

I wanted a slightly fancier version of the environment printer:  it
should label what it's printing as an environment.  But I don't want
the ugly class attribute to show, so I came up with this:

> print.foo
function(x){
attributes(x) _ NULL
print.default(x)
print(ls(env=x))
}

Before I set the class, I see this:

> .GlobalEnv
<environment: R_GlobalEnv>

Now I set the class, and my new print method is activated.  Looks just
the way I wanted:

> class(.GlobalEnv) _ 'foo'
> .GlobalEnv
<environment: R_GlobalEnv>
[1] "d"         "print.foo" "ranges"    "variances" "x"         "y"


However, it only works once, because I've lost the attributes from
.GlobalEnv, not just from my local copy of it:

> .GlobalEnv
<environment: R_GlobalEnv>

Shouldn't the line "attributes(x) _ NULL" have applied only to x, the
local copy of .GlobalEnv?

Duncan Murdoch
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list