[R] sequencing environments

Duncan Murdoch murdoch.duncan at gmail.com
Thu Feb 16 14:25:47 CET 2012


On 12-02-15 11:58 PM, Ben quant 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...

I'd advise thinking really carefully about this, because I think it 
indicates you've got a mental model of R internals that isn't a good 
match to what's really going on.  The kinds of things you might think about:

Would you find it just as strange that you can't easily get a list of 
all character vectors?  Environments aren't really more special than 
other objects.

If you think it's strange that you can't get a list of all character 
vectors, then consider that many of them aren't bound to names, so 
they'll have a C-level pointer to identify them, but they'll be hard to 
get to from within R.

The memory manager has a list of all objects and it goes through this 
list during garbage collection, but that's not something that user-level 
code should be able to do (except possibly debugging code).


>
> 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/
>
> ...do you (or anyone else) see any gotchas here? Any suggestions, help,
> things to watch for are welcome...

I would recommend that you avoid inventing another object system.  R 
currently has too many of those, and it means that lots of good code is 
unreadable by people who use a different one than you chose.

Gabor gave a list of existing systems based on environments; besides 
those you should consider the S4 system without reference classes (from 
the methods package).

I don't know any of them well enough to tell you how to choose, but I 
think if you put together prototypes of your project you'll run into 
problems with any of them, and a big part of your choice should be to 
see how easy it is to resolve those problems:  Do you get good help from 
the documentation?  Do you get good help on the mailing lists or 
Stackoverflow?


Duncan Murdoch

> Note: I am aware of the (surprising?) scoping rules.
>
> Thanks so much for your help.
>
> Ben
>
> On Tue, Feb 14, 2012 at 5:04 AM, Duncan Murdoch<murdoch.duncan at gmail.com>wrote:
>
>> On 12-02-14 12:34 AM, Ben quant wrote:
>>
>>> Hello,
>>>
>>> I can get at environments if I know their names, but what if want to look
>>> at what environments currently exist at some point in a script? In other
>>> words, if I don't know what environments exist and I don't know their
>>> sequence/hierarchy, how do I display a visual representation of the
>>> environments and how they relate to one another?
>>>
>>
>> Environments are objects and most of them are maintained in the same
>> places as other objects (including some obscure places, such as in
>> structures maintained only in external package code), so it's not easy to
>> generate a complete list.
>>
>>
>>
>>> I'm looking at getting away from the package R.oo and using R in normal
>>> state, but I need a way to "check in on" the status and organization of my
>>> environments.
>>>
>>> I've done considerable research on R's environments, but its a challenging
>>> thing to google and come up with meaningful results.
>>>
>>
>> I would suggest reading the technical documentation:  the R Language
>> manual, the R Internals manual, and some of the papers on the "Technical
>> papers" page.
>>
>> Duncan Murdoch
>>
>



More information about the R-help mailing list