Notes on using renv to manage virtual environments in R.
This seems much more straightforward than python so it's all below in the README.
renv
is not part of the base R installation. It is a contributed package available from CRAN (the Comprehensive R Archive Network), just like dplyr
or ggplot2
.
Check whether renv is installed by trying to load it with the library()
function.
library(renv)
What the results mean:
-
If it's INSTALLED: The command will run silently without any output, or it might print a startup message from the package. You won't get an error.
-
If it's NOT INSTALLED: You will get a clear error message.
Error in library(renv) : there is no package called ‘renv’
You only need to do this once on your computer. You install it into your main, global R library like any other package.
Open your R or RStudio console and run:
install.packages("renv")
Once this command is finished, the renv
package is available for you to use in any of your R projects.
You only need to do this once when you start a new project or decide to use renv
with an existing one.
- Navigate to your project's root directory in the R console.
- Run the
init()
command.
# Run this once per project
renv::init()
This command will:
- Create a project-specific library inside a new
renv/
folder. - Create or modify the project's
.Rprofile
file to ensurerenv
is loaded automatically whenever you open the project. - Discover the packages you are currently using in your scripts, install them into the new library, and create an initial
renv.lock
file by taking a snapshot.
Note that renv
might install way more packages than you were expecting it to - that is because it installs all the package dependencies too.
As you work on your project, you'll install new packages. You can do this in the standard way. renv
is smart enough to intercept this.
# Install a new package
install.packages("dplyr")
# Or, install multiple packages
install.packages(c("ggplot2", "stringr"))
# To remove a package
remove.packages("some_old_package")
These packages will be installed into your project's private renv
library, not your global R library.
After you have installed, updated, or removed packages and are happy with the state of your project, you must save these changes to the lockfile. This is the crucial step for reproducibility.
# Save the current state of your project library to renv.lock
renv::snapshot()
When you run this, renv
will:
- Scan your R scripts for required packages.
- Compare those packages with the state of your project library.
- Show you which packages will be added, removed, or changed in
renv.lock
. - Ask for your confirmation before writing to the lockfile.
Best Practice: Think of
renv::snapshot()
likegit commit
. You do it whenever you've reached a stable, meaningful state in your project's dependencies. You should addrenv.lock
to your Git repository.
This is the "payoff" step. Imagine a collaborator (or your future self on a new computer) has just cloned your project from Git. The repository contains your R scripts and the renv.lock
file.
To get the exact same package environment, they just need to:
- Open the project in R or RStudio.
- Run the
restore()
command. (Ifrenv
isn't activated it it will prompt you to activaterenv
first).
# Read renv.lock and install packages to match it
renv::restore()
renv
will read renv.lock
, see which packages are needed (and their exact versions), and install them. Your collaborator now has a perfect replica of your package environment, avoiding "it works on my machine" problems.
renv::status()
: Shows you the difference between the packages defined in yourrenv.lock
and the packages currently installed in your library. It's great for checking if you need to runsnapshot()
orrestore()
.renv::history()
: Shows the history of changes to the lockfile.renv::revert()
: Reverts changes to the lockfile, similar togit revert
.