RaukR 2025 • R Beyond the Basics
Roy Francis
08-May-2025
R package
installed.packages()
R library
.libPaths()
R repository
getOption("repos")
Unable to reproduce the same results when
Can you answer these questions about your project?
renv
is a toolkit to manage project-specific libraries of R packages
Isolate projects
Reproducible
Portable
Initialize
renv::init()
Write code install packages
install.packages()
Record environment
renv::snapshot()
Repeat 2 & 3
Restore environment if needed
renv::restore()
> renv::init()
renv: Project Environments for R
Welcome to renv! It looks like this is your first time using renv.
This is a one-time message, briefly describing some of renv's functionality.
renv will write to files within the active project folder, including:
- A folder 'renv' in the project directory, and
- A lockfile called 'renv.lock' in the project directory.
In particular, projects using renv will normally use a private, per-project
R library, in which new packages will be installed. This project library is
isolated from other R libraries on your system.
In addition, renv will update files within your project directory, including:
- .gitignore
- .Rbuildignore
- .Rprofile
Finally, renv maintains a local cache of data on the filesystem, located at:
- "~/Library/Caches/org.R-project.R/R/renv"
This path can be customized: please see the documentation in `?renv::paths`.
Please read the introduction vignette with `vignette("renv")` for more information.
You can browse the package documentation online at https://rstudio.github.io/renv/.
Do you want to proceed? [y/N]: y
- "~/Library/Caches/org.R-project.R/R/renv" has been created.
- Linking packages into the project library ... [32/32] Done!
The following package(s) will be updated in the lockfile:
# CRAN -----------------------------------------------------------------------
- R6 [* -> 2.5.1]
- base64enc [* -> 0.1-3]
- bslib [* -> 0.6.1]
- cachem [* -> 1.0.8]
- fastmap [* -> 1.1.1]
- rmarkdown [* -> 2.25]
- sass [* -> 0.4.8]
- vctrs [* -> 0.6.5]
- xfun [* -> 0.41]
- yaml [* -> 2.3.8]
The version of R recorded in the lockfile will be updated:
- R [* -> 4.3.2]
- Lockfile written to "~/Downloads/test/renv.lock".
- renv activated -- please restart the R session.
What has changed in my project compared to the previous record?
> renv::status()
The following package(s) are missing:
package installed recorded used
dplyr n n y
ggplot2 n n y
shiny n n y
See ?renv::status() for advice on resolving these issues.
> renv::snapshot()
The following required packages are not installed:
- dplyr
- ggplot2
- shiny
Packages must first be installed before renv can snapshot them.
Use `renv::dependencies()` to see where this package is used in your project.
What do you want to do?
1: Snapshot, just using the currently installed packages.
2: Install the packages, then snapshot.
3: Cancel, and resolve the situation on your own.
> renv::snapshot()
The following package(s) will be updated in the lockfile:
# RSPM -----------------------------------------------------------------------
- dplyr [* -> 1.1.4]
- fansi [* -> 1.0.6]
- generics [* -> 0.1.3]
- tidyselect [* -> 1.2.0]
- utf8 [* -> 1.2.4]
- withr [* -> 3.0.0]
Do you want to proceed? [Y/n]:
# record current state of packages
renv::snapshot()
{
"R": {
"Version": "4.3.2",
"Repositories": [
{
"Name": "CRAN",
"URL": "https://p3m.dev/cran/latest"
}
]
},
"Bioconductor": {
"Version": "3.18"
},
"remotes": {
"Package": "remotes",
"Version": "2.4.2.1",
"Source": "Repository",
"Repository": "RSPM",
"Requirements": [
"R",
"methods",
"stats",
"tools",
"utils"
],
"Hash": "63d15047eb239f95160112bcadc4fcb9"
}
}
> renv::restore()
The following package(s) will be updated:
# CRAN -----------------------------------------------------------------------
- R6 [* -> 2.5.1]
- fontawesome [* -> 0.5.2]
- xfun [* -> 0.41]
- yaml [* -> 2.3.8]
# RSPM -----------------------------------------------------------------------
- dplyr [* -> 1.1.4]
- fansi [* -> 1.0.6]
- withr [* -> 3.0.0]
Do you want to proceed? [Y/n]:
renv::restore()
# view lockfile history
renv::history()
# revert to previous state
renv::revert(commit="commit 2")
Without renv
Project specific library without global cache
renv::settings$use.cache(FALSE)
disables global cacheProject specific library with global cache
OS | Location |
---|---|
Linux | ~/.local/share/env |
MacOS | ~/Library/Application Support/renv |
Windows | %LOCALAPPDATA%/renv |
RENV_PATHS_CACHE
Set this environment variable to share package cache across multiple users
renv::activate(profile = "dev")
> renv::install(c("dplyr"))
# Downloading packages -------------------------------------------------------
- Downloading dplyr from CRAN ... OK [file is up to date]
- Downloading generics from CRAN ... OK [file is up to date]
- Downloading pillar from CRAN ... OK [file is up to date]
- Downloading fansi from CRAN ... OK [file is up to date]
- Downloading utf8 from CRAN ... OK [file is up to date]
- Downloading tibble from CRAN ... OK [file is up to date]
- Downloading pkgconfig from CRAN ... OK [file is up to date]
- Downloading tidyselect from CRAN ... OK [file is up to date]
- Downloading withr from CRAN ... OK [file is up to date]
Successfully downloaded 9 packages in 5.8 seconds.
The following package(s) will be installed:
- dplyr [1.1.4]
- fansi [1.0.6]
- generics [0.1.3]
- pillar [1.9.0]
- pkgconfig [2.0.3]
- tibble [3.2.1]
- tidyselect [1.2.0]
- utf8 [1.2.4]
- withr [3.0.0]
These packages will be installed into "~/Downloads/test/renv/library/R-4.3/x86_64-apple-darwin13.4.0".
Do you want to proceed? [Y/n]:
renv::snapshot()
renv::snapshot(type="all")
renv::dependencies()
renv::settings$snapshot.type("explicit")
renv::settings$ignored.packages("<package>")
renv::restore()
renv::settings$use.cache(FALSE)
renv::install()
renv::update()
renv::deactivate()
renv::activate()
renv::deactivate(clean=TRUE)
unlink(renv::paths$root(), recursive=TRUE)
Key functions
renv::init()
renv::snapshot()
renv::restore()
Useful
renv::status()
renv::dependencies()
renv::history()
renv::revert()
Project environments for R, Kevin Ushey, RStudio::Conf 2020
Reproducible environments with renv, Ryan Johnson, NHS-R community 2023
renv official documentation