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 versionsrenv::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/: holdsactivate.Rand the privatelibrary/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.