Basic renv Commands in R
• renv::init()
Initialize a new renv project in the current working directory.
• renv::install(<"pkg">)
Install a package into the renv project library.
• renv::snapshot()
Record the state of the project library into the lockfile renv.lock.
• renv::restore()
Restore the project library from the renv.lock file.
• renv::remove("pkg")
Remove a package from the renv project library.
• renv::update()
Update packages in the project library.
• renv::upgrade()
Upgrade the version of renv used in the project.
• renv::dependencies()
Discover package dependencies in project files.
• renv::status()
Show the status of the project environment compared to the lockfile.
• renv::load()
Report if any issues are detected when starting an renv project.
• renv::hydrate()
Installs missing packages from the user library into the project library.Automatically by init()
1
Common pitfalls with renv
• Packages disappearing
Not using renv.
• Forgetting to renv::snapshot()
Anyone picking up code later will be out of sync. renv::status() checks if packages and renv.lock
match.
• Switching branches
If different package requirements exist in branches, use renv::restore() when switching between
them, otherwise the library reflects the previous branch.
• Initialising renv outside a project
renv asks not to do this, do not use force = TRUE.
• Stuck on old CRAN/MRAN
Packages/versions you know exist won’t appear using install functions. Run
options(repos = "https://cloud.rproject.org/").
renv tips and tricks
• Total mess
Start again. Run renv::deactivate() and then delete the renv.lock file and the renv/ folder.
• Add package from GitHub
Use renv::install("username/packagename"). For a private package run
renv::install("git@github.com:username/packagename.git").
• Upgrade all packages to latest
Run renv::update() or renv::update("pkg") for a specific package. Always check that upgrading
packages does not break your code before pushing to GitHub for other users.
• Update renv
Run renv::upgrade(). Useful if renv gains new functionality to use.
• Error in file(filename, "r", encoding = encoding) : cannot open the connection
Accidentally installed renv in the home directory. Delete all of the files created by renv from the home
directory and retry.
• cannot open file ‘renv/activate.R’: No such file or directory
renv was installed incorrectly in the project. Go to Tools > Project Options > Environments and
tick Use renv with this project; this will force it to install renv, which should fix the problem.
2
Starting a new project with renv
Basic commands to follow to install packages for renv are:
# Install renv (if not already installed)
install.packages("renv")
# If starting a fresh repository, run this:
renv::init(bare = TRUE)
# If starting a fresh repository but would like to move existing packages over to renv:
renv::init()
Ensure the relevant files (.Rprofile, renv.lock, and renv/activate.R) were committed and pushed to the
GitHub repository. These should be the only files which git suggests to commit, do not commit the whole
contents of the renv folder created when initialising a project.
Working on a renv project
Work on the project as normal now, but when new packages are installed “snapshot” the packages to save
the state of the package environment. For instance, to install dplyr and update the package environment
then the process would be:
# Install a package into the renv project library
install.packages("dplyr")
# # Or use renv to install (the default is the latest available)
renv::install("dplyr")
# or install a specific version of a package
renv::install("dplyr@0.8.5")
# Load and use the package
library(dplyr)
# snapshot the state of the library to renv.lock
renv::snapshot()
# don’t forget to commit
# renv.lock!
Picking up a renv project
To pick up someone else’s project from GitHub who has been using renv run renv::restore() to update
the local package environment so it matches the renv.lock file.
# clone the project into
# Rstudio
# Restore packages from lockfile
renv::restore()
3
Any time a commit is pulled where the renv.lock file has changed, run renv::restore() in order to make
sure the package environment matches to the new renv.lock file. Also do this if you change branches in
your repository to one with a different renv.lock file.
# Check environment status at any time:
renv::status()
4