renv – Managing R Packages and Environment
by Vamshidhar Gangu, Research Computing, NUS Information Technology
Reproducibility challenge
One of the major complaints within R community is that it is very hard to maintain project level package dependencies as R is continuously evolving with several releases within a year.
While other counterpart programming languages often use virtual environments and package management tools like conda, similar tools or methods were not well adopted within the R community. One must either choose not to update the latest changes so as to maintain stable and reproducible environments or risk to update to the latest versions which often conflict and breaks the existing working environments.
The renv package is a new effort to bring project-local R dependency management to your projects. This is a robust, stable replacement for the Packrat package, with fewer surprises and better default behaviours.
renv
RStudio recently introduced renv package to manage local dependency and environment. It is a new package that is:-
- Simple to use in new or existing projects and
- Does not interrupt existing workflows
It helps to manage library paths and other projects specific state, to help isolate your project’s R dependencies while allowing you to use the existing tools like install.packages() and remove.packages() for managing R packages.
Workflow
The general workflow when working with renv is: –
- Call renv::init() to initialize a new project-local environment with a private R library
- Work in the project as normal, installing and removing new R packages as they are needed in the project
- Call renv::snapshot() to save the state of the project library to the lockfile (called lock)
- Continue working on your project, installing and updating R packages as needed
- Call renv::snapshot() again to save the latest state of your project library or call renv::restore() to revert to the previous state as encoded in the lockfile
Advantages of renv
- renv package is compatible with almost anywhere your team gets their packages (CRAN, github, Bioconductor, RStudio package manager)
- Can integrate with pipenv and reticulate for multilingual projects
- When compared with packrat, it is an improvementover managing package dependencies. renv also saves space by not installing the same version of a package if it has already been installed for another project
Compared with Python
There are lot of similarities between renv and python virtual environment (venv)
Task |
renv |
conda |
Pip |
Creating environment |
renv::init() |
conda create |
virtualenv |
Saving environment |
renv::snapshot() |
conda env export > env.yml |
pip freeze >reqs.txt |
Loading environment |
renv::restore() |
conda env create -f env.yml |
pip install -r reqs.txt |
References
- Renv document: https://rstudio.github.io/renv/articles/renv.html
- Renv github: https://github.com/rstudio/renv/