Virtual environment in R?
Asked Answered
A

6

93

I've found several posts about best practice, reproducibility and workflow in R, for example:

One of the major preoccupations is ensuring portability of code, in the sense that moving it to a new machine (possibly running a different OS) is relatively straightforward and gives the same results.

Coming from a Python background, I'm used to the concept of a virtual environment. When coupled with a simple list of required packages, this goes some way to ensuring that the installed packages and libraries are available on any machine without too much fuss. Sure, it's no guarantee - different OSes have their own foibles and peculiarities - but it gets you 95% of the way there.

Does such a thing exist within R? Even if it's not as sophisticated. For example simply maintaining a plain text list of required packages and a script that will install any that are missing?

I'm about to start using R in earnest for the first time, probably in conjunction with Sweave, and would ideally like to start in the best way possible! Thanks for your thoughts.

Arioso answered 18/6, 2014 at 8:35 Comment(7)
Good question. I voted for migrating this to SO. The question has no statistical content, so the SO people could probably give you a better answer.Sulcus
Thanks, and good point. I've requested a migration from the moderators.Arioso
Renv might do what you're looking for: github.com/viking/RenvBwana
Maintaining a list of packages and isolating them from the system library is exactly what packrat does for R: rstudio.github.io/packratBleachers
Someone should put an answer in a legit answer instead of the comments. I don't know enough R to answer, but from looking at the links, Renv appears to just manage versions of R while Packrat's docs sound more like a match for virtualenv in which you can use the same language version with different dependency sets in different projects.Vascular
"probably in conjunction with Sweave" --- Do yourself a favor and start with knitr, not Sweave.Zandra
@Arioso Please consider accepting a different answer, since the currently accepted one is outdated and misleading. The correct answer nowadays is https://mcmap.net/q/224532/-virtual-environment-in-r.Estes
S
36

It looks like there is another option from RStudio devs, renv. It's available on CRAN and supersedes Packrat.

In short, you use renv::init() to initialize your project library, and use renv::snapshot() / renv::restore() to save and load the state of your library.

I prefer this option to conda r-enviroments because here everything is stored in the file renv.lock, which can be committed to a Git repo and distributed to the team.

Skeg answered 4/3, 2020 at 15:25 Comment(2)
This answer should definitely be the accepted answer in our time.Despair
Fair warning, there are quite a few unresolved issues with renv and Bioconductor packages as of 5-26-21 (some of which I experienced first hand). github.com/rstudio/renv/issues?q=bioconductor . Kind of a deal breaker for me at the moment.Bigname
T
50

I'm going to use the comment posted by @cboettig in order to resolve this question.

Packrat

Packrat is a dependency management system for R. Gives you three important advantages (all of them focused in your portability needs)

  • Isolated : Installing a new or updated package for one project won’t break your other projects, and vice versa. That’s because packrat gives each project its own private package library.

  • Portable: Easily transport your projects from one computer to another, even across different platforms. Packrat makes it easy to install the packages your project depends on.

  • Reproducible: Packrat records the exact package versions you depend on, and ensures those exact versions are the ones that get installed wherever you go.

What's next?

  1. Walkthrough guide: http://rstudio.github.io/packrat/walkthrough.html

  2. Most common commands: http://rstudio.github.io/packrat/commands.html

  3. Using Packrat with RStudio: http://rstudio.github.io/packrat/rstudio.html

  4. Limitations and caveats: http://rstudio.github.io/packrat/limitations.html

Update: Packrat has been soft-deprecated and is now superseded by renv, so you might want to check this package instead.

Teressaterete answered 5/8, 2014 at 17:0 Comment(3)
What would be the Packrat way of capturing your dependencies in a requirements.txt file and installing then via pip install -r ...?Phyla
I don't know why but packrat is extremely slow to install packages. Literally lost two days for every time I tried to download a new packageTory
@Nick The docs here show a snapshot command for that.Bani
S
36

It looks like there is another option from RStudio devs, renv. It's available on CRAN and supersedes Packrat.

In short, you use renv::init() to initialize your project library, and use renv::snapshot() / renv::restore() to save and load the state of your library.

I prefer this option to conda r-enviroments because here everything is stored in the file renv.lock, which can be committed to a Git repo and distributed to the team.

Skeg answered 4/3, 2020 at 15:25 Comment(2)
This answer should definitely be the accepted answer in our time.Despair
Fair warning, there are quite a few unresolved issues with renv and Bioconductor packages as of 5-26-21 (some of which I experienced first hand). github.com/rstudio/renv/issues?q=bioconductor . Kind of a deal breaker for me at the moment.Bigname
B
32

The Anaconda package manager conda supports creating R environments.

conda create -n r-environment r-essentials r-base
conda activate r-environment

I have had a great experience using conda to maintain different Python installations, both user specific and several versions for the same user. I have tested R with conda and the jupyter-notebook and it works great. At least for my needs, which includes RNA-sequencing analyses using the DEseq2 and related packages, as well as data.table and dplyr. There are many bioconductor packages available in conda via bioconda and according to the comments on this SO question, it seems like install.packages() might work as well.

Broddy answered 13/5, 2015 at 18:58 Comment(3)
I have also moved into bioinformatics now, so your answer becomes even more relevant! What IDE do you use? I've had trouble integrating conda into Rstudio - it doesn't want to use the correct interpreter path.Arioso
Fun! I use the jupyter-notebook for most of my needs. I did use Rstudio previously, but I remember also having some trouble getting it running with conda and I am used to the notebook from python (which is what I use most frequently by far), so that's where I feel the most comfortable.Broddy
My conda R was still showing my non-conda R's library location in .libPaths() and trying to install new packages there, I had to run export R_LIBS_USER="/path/to/miniconda2/envs/r.3.3/lib/R/library" in the terminal before starting R and trying to install packages. Still testing but it looks like its working.Disturbing
D
1

To add to this:

Note: 1. Have Anaconda installed already 2. Assumed your working directory is "C:"

To create desired environment -> "r_environment_name"

C:\>conda create -n "r_environment_name" r-essentials r-base

To see available environments

C:\>conda info --envs

. .. ...

To activate environment

C:\>conda activate "r_environment_name"

(r_environment_name) C:\>

Launch Jupyter Notebook and let the party begins

(r_environment_name) C:\> jupyter notebook

For a similar "requirements.txt", perhaps this link will help -> Is there something like requirements.txt for R?

Drunken answered 21/5, 2019 at 9:24 Comment(0)
S
0

Check out roveR, the R container management solution. For details, see https://www.slideshare.net/DavidKunFF/ownr-technical-introduction, in particular slide 12.

To install roveR, execute the following command in R:

install.packages("rover", repos = c("https://lair.functionalfinances.com/repos/shared", "https://lair.functionalfinances.com/repos/cran"))

To make full use of the power of roveR (including installing specific versions of packages for reproducibility), you will need access to a laiR - for CRAN, you can use our laiR instance at https://lair.ownr.io, for uploading your own packages and sharing them with your organization you will need a laiR license. You can contact us on the email address in the presentation linked above.

Stillbirth answered 17/6, 2017 at 10:59 Comment(4)
I think this resource no longer existsCombined
Yeah, the slideshare is gone. I can't connect to the two repositories in this answer either, nor lair.ownr.io .Counterrevolution
Sorry guys, laiR is temporarily offline, we are working on integrating it into our other product (the ownR platform) and will make it available again asap.Stillbirth
content not available, seems that this isn't an answer at the current timeExpectorant
F
0

For .ipynb files a R kernel is enough, you do NOT need to install a virtual environment with R, just:

Fantom answered 25/2, 2023 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.