4 Manage Packages and Environment with Ease


R package dependencies can be frustrating. Have you ever had to use trial-and-error to figure out what R packages you need to install to make someone else’s code work–and then been left with those packages globally installed forever, because now you’re not sure whether you need them? Have you ever updated a package to get code in one of your projects to work, only to find that the updated package makes code in another project stop working?


Python Environment xkcd 1987
Python Environment xkcd 1987

R is not that much better

4.1 conda

conda is an open source package management system and environment management system that runs on Windows, macOS, Linux and z/OS. Conda quickly installs, runs and updates packages and their dependencies. Conda easily creates, saves, loads and switches between environments on your local computer. It was created for Python programs, but it can package and distribute software for any language.

While it is possible to install and manage R packages through conda , but other R-specific package managers work better with r-studio. On the other hand, condais a convenient choice to install python packages and linux software - most bioinformatics tool are available on bioconda

4.2 renv

renv is a new effort to bring project-local R dependency management to your projects. The goal is for renv to be a robust, stable replacement for the Packrat package, with fewer surprises and better default behaviors.

4.2.1 General Workflow

  1. Call renv::init() to initialize a new project-local environment with a private R library,
  2. Work in the project as normal, installing and removing new R packages as they are needed in the project,
  3. Call renv::snapshot() to save the state of the project library to the lockfile (called renv.lock),
  4. Continue working on your project, installing and updating R packages as needed.
  5. Call renv::snapshot() again to save the state of your project library if your attempts to update R packages were successful, or call renv::restore() to revert to the previous state as encoded in the lockfile(renv.lock) if your attempts to update packages introduced some new problems.

Note: The renv::init() function attempts to ensure the newly-created project library includes all R packages currently used by the project. It does this by crawling R files within the project for dependencies with the renv::dependencies() function. The discovered packages are then installed into the project library with the renv::hydrate() function, which will also attempt to save time by copying packages from your user library (rather than reinstalling from CRAN) as appropriate.

4.2.2 Collaborating

  1. Select a way to share your project sources. We recommend using a version control system alongside a public repository; e.g. git with GitHub, but many other options are available.
  2. One user (perhaps yourself) should explicitly initialize renv in the project, via renv::init(). This will create the initial renv lockfile, and also write the renv auto-loaders to the project’s .Rprofile and renv/activate.R. These will ensure the right version of renv is downloaded and installed for your collaborators when they start in this project.
  3. Share your project sources, alongside the generated lockfile renv.lock. Be sure to also share the generated auto-loaders in .Rprofile and renv/activate.R.
  4. When a collaborator first launches in this project, renv should automatically bootstrap itself, thereby downloading and installing the appropriate version of renv into the project library. After this has completed, they can then use renv::restore() to restore the project library locally on their machine