Skip to the workspace
Pebellslearn by building
Introduction and Setup  ›  Capstone Project

renv — A Private Library per Project

renv 14 minrenv gives each project its own package library and a lockfile of exact versions, so snapshot() records a working setup and restore() rebuilds it on any machine.

r programming / renv — A Private Library per Project

Explore each scene at your own pace.

01 · The rule

The rule

renv — A Private Library per Project

renv gives each project its own package library and a lockfile of exact versions, so snapshot() records a working setup and restore() rebuilds it on any machine.

Record and restore project dependencies without assuming the whole machine is reproduced.

Last spring your harvest analysis ran perfectly. This autumn you updated your packages for another project, and now the old script throws errors, or worse, gives slightly different numbers. A new volunteer tries it on their laptop and hits a third set of problems. All of these have the same cause: every project was sharing one library of whatever versions happened to be installed.

The rule

renv freezes a project's package versions: snapshot to save, restore to rebuild.

renv is a package (install it once with install.packages("renv")) that gives each project its own private library plus a record of exactly which versions it uses. Three verbs cover everyday work:

renv::init()       # once: set up a private library for this project
renv::snapshot()   # after installing or updating: record the versions
renv::restore()    # on any machine: install exactly the recorded versions

renv::init() creates a few things in the project folder:

  • renv.lock: the lockfile, a small JSON file listing every package the project uses, with its exact version and where it came from.
  • .Rprofile: a start-up file that switches renv on whenever R opens this project.
  • renv/: holds activate.R and the private library/ folder.

An entry in the lockfile looks like this:

"dplyr": {
  "Package": "dplyr",
  "Version": "1.1.4",
  "Source": "Repository",
  "Repository": "CRAN"
}

Inside the project, install packages as usual. They go into the private library, so updating a package here cannot break another project, and updates elsewhere cannot break this one. renv::status() tells you when the library and the lockfile disagree, for example after you install something new but forget to snapshot.

For version control, commit renv.lock, .Rprofile and renv/activate.R. Do not commit renv/library/: it is large and specific to one machine, and renv's own .gitignore already leaves it out.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.