Last modified: 2026-10-11 02:07:28 (PDT)
Replicable code should produce the same results, regardless of when or where it’s run. However, our analyses often leverage open-source R packages that are developed by other teams. These packages continue to be developed after research projects are completed, which may include changes to analysis functions that could impact how code runs for both other team members and external replicators.
For example, suppose we had used a function that took in one argument, such that our code contained example_function(arg_a = "a"). A few months after we publish our code, the package developers update the function to take in another mandatory argument arg_b. If someone runs our code, but has the most recent version of the package, they’ll receive an error message that the argument arg_b is missing and will not be able to fully reproduce our results.
To ensure that the right functions are used in replication efforts, it is important for us to keep track of package versions used in each project.
renv can be to promote reproducible environments within R projects. renv creates individual package libraries for each project instead of having all projects, which may use different versions of the same package, share the same package library. However, for projects that use many packages, this process can be memory intensive and increase the time needed for a new users to start running code.
This chapter provides a quick tutorial for integrating renv into research workflows. For more detailed instructions, please refer to the renv package vignette.
Ideally, renv should be initiated at the start of projects and updated continuously when new packages are introduced in the codebase. However, this process can be initiated at any point in a project
To add renv to your workflow, follow these steps:
renv package by running install.packages("renv")renv::init() to initialize renv in your R Projectrenv.lock, .Rprofile, renv/settings.json and renv/activate.R. Commit and push these files to GitHub so that they’re accessible to other users.renv::snapshot() in the R consolerenv::restore() to the head of your config file, to make sure that all users that run your code are on the same package versions.The renv/settings.json file created during initialization allows you to customize how renv behaves in your project. One useful setting is snapshot.dev, which controls whether development dependencies are included by default when calling renv::snapshot() or renv::status().
When working on projects, you may encounter startup messages indicating that renv is out of sync with the lockfile. To reduce these messages in most projects, add the following setting to renv/settings.json:
This setting (available in renv version 1.1.6 and later) includes development dependencies in snapshots by default, which helps keep the lockfile aligned with your actual usage and eliminates many synchronization warnings.
If startup messages about being out of sync persist after enabling this setting, use renv::restore() to sync your local library with the lockfile, or renv::snapshot() to update the lockfile with your current package versions.
For more details on renv configuration options, see the official renv documentation.
If you’re starting to work on an ongoing project that already has renv set up, follow these steps to ensure that you’re using the same project versions.
renv package by running install.packages("renv")renv::restore(). In our lab’s projects, this is often already found at the top of the config file, so you can just run scripts as is.renv lock file. You should now be able to replicate the code.For users working on Ubuntu Linux systems, r2u provides an alternative approach to package management that complements or replaces renv in certain workflows.
What is r2u?
r2u integrates R package installation with Ubuntu’s apt package manager, providing pre-compiled binary packages for all CRAN packages. This integration offers several advantages over traditional R package installation:
Supported Ubuntu versions (as of early 2025):
When to use r2u vs. renv:
Basic setup:
r2u requires a one-time system setup. See the r2u website for detailed installation instructions.
Once configured, R’s install.packages() automatically uses binary packages from r2u, making package installation much faster.
Docker containers:
The Rocker Project provides rocker/r2u Docker containers with r2u pre-configured, ideal for reproducible containerized workflows.
For complete documentation and setup instructions, see the r2u website.
rig is an R installation manager that makes it easy to install, manage, and switch between multiple versions of R on macOS, Windows, and Linux.
When working across projects that require different R versions, or when you need to match a specific R version for reproducibility, rig provides a simple command-line interface to handle R version management.
Key features:
rig add release, list installed versions with rig listWhen to use rig:
Basic usage:
For full installation instructions and documentation, see the rig GitHub repository.
When Docker images and containers accumulate on a server, they can consume significant disk space. To reclaim disk space by removing unused Docker data:
docker system prune -a.This command removes all stopped containers, all unused networks, all unused images (not just dangling ones), and all build cache.
Warning
docker system prune -a removes all unused images, not just dangling ones. Confirm that no important containers are currently stopped before running it, as their associated images will also be removed.