In the "What is the most useful R trick?" (here), I read that using environments give "pass-by-reference capabilities". Are there any limits and/or gotchas with this approach?
Also, in general what are the pros and cons of using created environments? This is something I've been confused about for quite some time, so any clarity or reference would be very helpful to me.
Thank you in advance.
One of your projects might require a different version of an external library than another one. If you have only one place to install packages, then you can't work with two different versions of the same library. This is one of the most common reasons for the recommendation to use a Python virtual environment.
A virtual environment is simply a tool that separates the dependencies of different projects by creating a separate isolated environment for each project. These are simply the directories so that unlimited virtual environments can be created. This is one of the popular tools used by most of the Python developers.
While I agree with Harlan's overall advice (i.e. don't use something unless you understand it), I would add:
Environments are a fundamental concept in R, and in my view, extremely useful (in other words: they're worth understanding!). Environments are very important to understand issues related to scope. Some basic things that you should understand in this context:
search()
: will show you the workspace; environments are listed in order of priority. The main environment is .GlobalEnv, and can always be referenced as such.ls()
: will show you what's contained in an environmentattach
/detach
: creates a new environment for an objectget
, assign
, <<-
, and <-
: you should know the difference between these functionswith
: one method for working with an environment without attaching it.Another pointer: have a look at the proto package (used in ggplot), which uses environments to provide controlled inheritance.
Lastly, I would point out that environments are very similar to lists: they can both store any kind of object within them (see this question). But depending on your use case (e.g. do you want to deal with inheritance and priority), a list can be easier to work with. And you can always attach
a list as an environment.
Edit: If you want to see an example of proto
at work in ggplot
, have a look that the structure of a ggplot object, which is essentially a list composed partially of environments:
> p <- qplot(1:10, 1:10) > str(p) List of 8 $ data :'data.frame': 0 obs. of 0 variables $ layers :List of 1 ..$ :proto object .. .. $ legend : logi NA .. .. $ inherit.aes: logi TRUE ... > class(p$layers[[1]]) [1] "proto" "environment" > is.environment(p$layers[[1]]) [1] TRUE
Notice how it's constructed using proto and is containing many environments as a result. You can also plot the relationships in these objects using graph.proto
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With