[R] sequencing environments

Gabor Grothendieck ggrothendieck at gmail.com
Thu Feb 16 07:09:19 CET 2012

On Wed, Feb 15, 2012 at 11:58 PM, Ben quant <ccquant at gmail.com> wrote:
> Thank you Duncan. Interesting. I find it strange that you can't get a list
> of the environments. But I'll deal with it...
> Anyway, I'm about to start a new R dev project for my company. I'm thinking
> about architecture, organization, and gotchas. I went through much of the
> documentation you sent me. Thanks!. I came up with what I think is the best
> way to implement environments (which I am using like I would use a class in
> a traditional OO language) that can be reused in various programs.
> I'm thinking of creating different scripts like this:
> #this is saved as script name EnvTest.R
> myEnvir = new.env()
> var1 = 2 + 2
> assign("myx",var1,envir=myEnvir)
> Then I will write programs like this that will use the environments and the
> objects/functions they contain:
> source("EnvTest.r")
> prgmVar1 = get("myVar1",pos=myEnvir)
> ## do stuff with env objects
> print(prgmVar1)
> Do you think this is the best way to use environments to avoid naming
> conflicts, take advantage of separation of data, organize scripting
> logically, etc. (the benefits of traditional OO classes)? Eventually, I'll
> use this on a Linux machine in the cloud using.:
> https://github.com/armstrtw/rzmq
> https://github.com/armstrtw/AWS.tools
> https://github.com/armstrtw/deathstar
> http://code.google.com/p/segue/

Reference classes, the oo.R package and the proto package provide OO
implementations based on environments.

Being particular familiar with the proto package
(http://r-proto.googlecode.com), I will discuss it.  The graph.proto
function in that package will draw a graphViz graph of your proto
objects (environments).  Using p and x in place of myEnv and myx your
example is as follows.

p <- proto(x = 2+2)
p$x  # 4

# add a method, incr
p$incr <- function(.) .$x <- .$x + 1
p$incr() # increment x
p$x # 5

# create a child
# it overrides x; inherits incr from p
ch <- p$proto(x = 100)
ch$x # 101

Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

More information about the R-help mailing list