Virtual environment in R?

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.


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.


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.


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?

Tags:

Python

R