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

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Jun 14 17:45:13 CEST 2000


murdoch at stats.uwo.ca (Duncan Murdoch) writes:

> It's also rather cumbersome to extract a member from an environment,
> you need the "get" function. It would probably make sense to overload
> $ to work on environments, so that instead of Doug Bates' code
> 
>   get("total", env = environment(ross$balance))
> 
> you could just type
> 
>   environment(ross$balance)$total
> 
> Print methods for environments would be nice; these should do an ls()
> of the environment and say what the parent environment is.

...both of which are (almost) possible already in 1.1.0 if you attach
a class attribute:

> .GlobalEnv  
> print.foo<-function(x)print(ls(envir=x))
> .GlobalEnv
[1] "$.environment"     "$.foo"             "print.environment"
[4] "print.foo"         "x"                
> class(.GlobalEnv)
[1] "foo"
> .GlobalEnv$"$.foo"
function(x,name)get(name,envir=x)

The print.environment and print.foo forms don't work. That would
require dispatch on the data class of the argument.


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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