Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when do you want to set up new environments in R

Tags:

r

per the discussion of R programming styles, I saw someone once said he puts all his custom function into a new environment and attach it. I also remember R environment maybe used as a hash table. Is this good style? When do you want to put your data/functions into a new enviroment? Or just use the.GlobalEnv whatever?

EDIT put my second part of question back: how to inspect same name variable for different environments?

like image 976
learnbasicR Avatar asked Dec 10 '10 19:12

learnbasicR


People also ask

What is the use of environment in RStudio?

The Environment tab provides meta-project information such as what values you have stored in variables and your custom functions. The History tab runs through your previously-executed commands.

How do I start a new environment in R?

To create an environment manually, use new. env() . You can list the bindings in the environment's frame with ls() and see its parent with parent. env() .

Which environment will R look up first?

When a function is evaluated, R looks in a series of environments for any variables in scope. The evaluation environment is first, then the function's enclosing environment, which will be the global environment for functions defined in the workspace.

When you want to make an object that was defined in your function accessible to the global environment what function would you use?

If you really want to assign to the global environment you can use the assign function and tell it explicitly that you want to assign globally.


2 Answers

Martin Mächler suggests that this is the one time you might want to consider attach(), although he suggested it in the context of attaching a .Rdata file to the search path but your Q is essentially the same thing.

The advantage is that you don't clutter the global environment with functions, that might get overwritten accidentally etc. Whilst I wouldn't go so far as to call this bad style, you might be better off sticking your custom functions into your own personal R package. Yes, this will incur a bit of overhead of setting up the package structure and providing some documentation to allow the package to be installed, but in the longer term this is a better solution. With tools like roxygen this process is getting easier to boot.

Personally, I haven't found a need for fiddling with environments in 10+ years of using R; well documented scripts that load, process and analyse data, cleaning up after themselves all the while have served me well thus far.


Another suggestion for the second part of your question (now deleted) is to use with() (following on from @Joshua's example):

> .myEnv <- new.env()
> .myEnv$a <- 2
> a <- 1
> str(a)
 num 1
> ls.str(.myEnv, a)
a :  num 2
> str(.myEnv$a)
 num 2
> with(.myEnv, a)
[1] 2
> a
[1] 1
like image 92
Gavin Simpson Avatar answered Oct 22 '22 11:10

Gavin Simpson


If your ecosystem of data and code has grown large enough that you are considering isolating it in an environment, you are better off creating a package. A package gives you much more support for:

  • Managing a project that is growing large and complex by separating code and data into files so there is less to dig through at one time.

  • A package makes it dead simple to hand off your work to someone else so they can use your code and data.

  • A package provides additional support for documentation and reporting.

Setting up a package for R is so easy, just call package.skeleton(), that every project I work on gets its code and data stored in a package.

The only time I use environments is when I need to isolate a run of some code, usually a script written by someone else, so that it's side effects and variable names don't get crossed with mine. I do this by evalq(source('someScript.R', local=TRUE), SomeEnvironment).

like image 22
Sharpie Avatar answered Oct 22 '22 09:10

Sharpie