Skip to Content
Course content

188: Collaborative R Development with renv and Git

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I can't tell you how many times I've seen a project grind to a halt because of the phrase, "It works on my machine." You've probably been there: you spend three days polishing a complex analysis, push your code to Git, and then your colleague pulls it down only to be greeted by a wall of red error text because they have dplyr version 1.1.0 while you wrote everything for 1.0.10. Or maybe they're missing a niche dependency like sf that requires a system-level library they haven't installed.

The "Just Install These" Approach

The naive way to handle collaboration is to include a README.md file with a list of required packages. You might even write a little install.packages(c("tidyverse", "data.table", "randomForest")) script. On the surface, this feels efficient. You aren't bloating your Git repo with binary files, and you're giving the other person a starting point. But this is a fragile system. It assumes that everyone is using the same version of R and that the CRAN mirrors are serving the exact same package versions to everyone at the same moment.

I've seen this break in spectacular ways. Imagine you're working on a climate model using ggplot2. You use a specific feature of a recent update. Your teammate, who hasn't updated their environment in six months, tries to run your script. They get a "function not found" error. They update ggplot2 to fix it, but that update breaks a different, older package they need for another project. Now they're chasing dependency ghosts instead of actually analyzing data. The cost here isn't just time; it's the mental friction that makes people dread collaborating.

Locking Down the Environment

The professional way to handle this is by using renv. Instead of a list of names in a text file, renv creates a project-local library. It essentially tells R, "For this specific folder, don't use the global library where everything is a mess; use this private stash of packages."

The magic happens with the renv.lock file. This is a JSON file that records the exact version and source of every package your project depends on. When you run renv::snapshot(), R scans your code, figures out what you're actually using, and writes those exact versions into the lockfile. Now, when you commit that lockfile to Git, you aren't just sharing code; you're sharing a blueprint of the entire environment.

# Start the environment
renv::init()

# After installing your packages and writing your code
renv::snapshot() 

When your colleague pulls the repo, they don't guess which packages to install. They simply run renv::restore(). R looks at the lockfile and installs the exact versions you used. It’s the difference between giving someone a recipe that says "use some flour" and giving them a specific brand and weight of flour. The result is consistent every single time.

The Git-renv Dance

There is one specific detail that trips people up when they first combine renv with Git: what actually gets committed. You should never commit the renv/library folder. That folder contains the actual installed binaries, which are huge and OS-specific (a Windows library won't work on a Mac).

Instead, renv automatically creates a .gitignore file that tells Git to ignore the library. You only commit the renv.lock file and the .Rprofile. This keeps your repository lightweight while remaining fully reproducible. The workflow becomes a rhythm: you add a package, you snapshot(), you commit the lockfile. Your teammate pulls the lockfile, they restore(), and they're exactly where you left off.




πŸ“‹ Practical Task

Synchronizing the Global Weather Dataset Environment

You have been handed a legacy project called global_weather_analysis. The project is currently a mess: it has no environment management, and the current lead developer just changed the dplyr version, breaking the code for everyone else.

Your goal: Establish a reproducible environment using renv so the team can stop fighting over package versions.

  1. Create a new directory named global_weather_analysis and open it as an R Project.
  2. Initialize renv in this project using renv::init().
  3. Install the following packages: dplyr, ggplot2, and tidyr.
  4. Write a small script analysis.R that loads these libraries and performs a simple operation (e.g., creating a dummy data frame and filtering it).
  5. Run renv::snapshot() to capture the state of your library into the renv.lock file.
  6. Verify that the renv.lock file has been created in your project root and contains the version numbers for the three packages you installed.
  7. The Test: Delete your local project library folder (renv/library) to simulate a fresh clone from Git. Run renv::restore() and verify that R successfully reinstalls the exact versions required by the lockfile without you having to call install.packages() manually.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.