Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

renv — A Private Library per Project

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.

The shortcut

Think of renv.lock as a recipe card. snapshot() writes the card from what is in your kitchen right now; restore() cooks the same dish in someone else's kitchen from the card. So: init once, snapshot after every change, restore on arrival.

Handing the garden project to a new volunteerStep 1 of 4
  1. You: set up

    In the project folder, run renv::init(). The project now has its own library and a lockfile.

The classic slip is installing a package and forgetting to snapshot. Your code works, but the lockfile does not mention the package, so the volunteer's restore leaves it out. renv::status() catches this.

Drills

Answer fast — trust the rule.

quick drill

Which file records the exact package versions of an renv project?

quick drill

You installed a new package in an renv project and used it in a script. What do you run so teammates get it too?

quick drill

A teammate has just cloned the project. Which command installs the recorded versions?

quick drill

Which of these should NOT be committed to version control?

quick drill

Why does renv give every project its own library?

Remember

  • renv freezes a project's package versions: snapshot to save, restore to rebuild.
  • Shortcut: the lockfile is a recipe card. Init once, snapshot after changes, restore on arrival.
  • Trap: installing without snapshotting, and committing renv/library/.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainMake it run on their laptop

You are handing the garden analysis project to a new volunteer. Explain, step by step, how you would use renv so the code runs with the same package versions on their laptop, and which files they need from you.