Project Environments - Introduction

The main tool for project environment handling in R is the package {renv}.

“The renv package 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.” – from the {renv} package vignette

If you are not familiar with the {renv} approach the introductory package vignette “Introduction to renv” is a good starting point. While the use of Package Manager snapshots within Workbench instances (see article Package Manager snapshots) configured by cynkra mitigates version management issue substantially, we highly recommend the use of {renv} for every project.

{renv} allows full control of a project’s R (package) environment. This makes a project reproducible and allows collaborators to get started easily as they only need to call renv::restore() to install all required dependencies. In addition, thanks to the shared {renv} cache (see section below), package installation is almost instant on Workbench instances as packages get symlinked instead of downloaded and reinstalled.

Furthermore the use of {renv} makes it easier for Connect to install dependencies and reduces the risk of running into incompatibility during publication.

{renv} Helpers in {cynkrathis}

The {cynkrathis} package has some {renv} related helper functions to make {renv} project management easier. For example, cynkrathis::renv_switch_r_version() allows for a simplified R version upgrade of an {renv} project which is coupled to an Package Manager snapshot. Assuming you want to bump an {renv} project running on R 4.1.3 to R 4.2.1, you can call renv_switch_r_version("4.2.1") and the function will replace the “repos” definition in renv.lock with the corresponding Package Manager snapshot of the respective R version.

Git diff after applying cynkrathis::renv_switch_r_version("4.2.1")

Shared Cache Configuration

cynkra has configured a shared {renv} cache for all Workbench instances1.

renv:::renv_paths_root()
[1] "/opt/R/renv"

renv:::renv_paths_cache()
[1] "/opt/R/renv/cache/v5/linux-ubuntu-focal/R-4.2/x86_64-pc-linux-gnu"

This way, users can make use of a (large) shared cache which is filled by all users on the system without having to build their own cache. This helps to reduce redundant files on the system as all users can symlink from this shared cache within their {renv} projects.

When a user joins an existing Workbench installation, chances are high that many packages the user installs via renv::install() are already present in the cache and will hence be instantly available by symlinking them instead of downloading and installing them from scratch (as install.packages() would do).

Footnotes

  1. By default, {renv} places its cache on the user-level, i.e., at ~/.local/ which means that each user would only profit from their own cache.↩︎