Creating and maintaining project-relative package libraries with renv
Leibniz-Zentrum Allgemeine Sprachwissenschaft
Thu Oct 17, 2024
renv package: creating a project-relative package libraryrenv website
install.packages(), as we’ve been doingpacman package (optional)
p_load() function to replace install.packages() and library() in our worksflow
library())install.packages()) and then loaded (as with library())pacman has a function for developer packages (which we’ll talk about later)To get started: install pacman (install.packages("pacman")). Then, you can load in your packages using pacman::p_load(), or with a long list of library() calls like we’ve previously done (you see why I prefer p_load()!).
The additional benefit of p_load() is that, if you don’t actually have one of the packages installed it will automatically be installed and then loaded. With library() you would instead get an error message.
install.packages()
packageVersion("package")
Tools > Check for package updates, orupdate.packages()
.libPaths()
> .libPaths()
[1] "/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library"
renv package to do thisrenv packageReproducible Environments for R projectsrenv aids in maintaining reproducible environments in R projects (Ushey & Wickham, 2024)
renv freezes and stores package versions used in a projectrenv
renv…
…can
…cannot
renv workflowrenv
Figure 1: Source: CRAN vignette ‘Introduction to renv’ (all rights reserved)
#| eval: false
renv, I would keep this in a code chunk with #| eval: false
- Linking packages into the project library ... [137/137] Done!
- Resolving missing dependencies ...
# Installing packages --------------------------------------------------------
The following package(s) will be updated in the lockfile:
# CRAN -----------------------------------------------------------------------
[long list of packages and their versions]
The version of R recorded in the lockfile will be updated:
- R [* -> 4.4.0]
- Lockfile written to "~/Documents/IdSL/Teaching/SoSe24/M.A./r4repro_student/renv.lock".
Restarting R session...
- Project '~/Documents/IdSL/Teaching/SoSe24/M.A./r4repro_student' loaded. [renv 1.0.7]
renv::init() creates three new files or directories
renv.lockrenv/.Rprofilerenv.lockR: info on R version and list of repositories where packages were installed fromPackages: a record per package with necessary info for re-installationrenv/library/
.RProfile.libPaths(), we should see our project library[1] is the local project library path[2] is the path to a global package cache that renv maintains so that you don’t repeatedly download packages to your machine for each project library
ggplot2 installed globally on our machine, whenever we want to add it to a project library we don’t need to re-install it entirely from the CRAN (unless we want a different package version)renv.lock?
lme4 (Bates et al., 2015)
beepr, which can play notification sounds (Bååth, 2024)
beepr:install_github() function from either the remotes or devtools package (both are very common)renv::install()
renv.lock)> renv::status()
No issues found -- the project is in a consistent state.
renv.lock filerenv, we can use:lock = TRUE
check = T
some other packages that can be useful for package management or reproducibility
groundhog: version control for CRAN, GitHub, and GitLab packages
groundhog.library() instead of library() to load packagesissues can arise when package versions were built on a previous version of R, and are no longer supported
renv)renv or not, always end a script with sessionInfo()
R version 4.4.1 (2024-06-14)
Platform: aarch64-apple-darwin20
Running under: macOS Sonoma 14.6
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: Europe/Berlin
tzcode source: internal
attached base packages:
[1] stats graphics grDevices datasets utils methods base
loaded via a namespace (and not attached):
[1] digest_0.6.35 fastmap_1.2.0 xfun_0.45 magrittr_2.0.3
[5] knitr_1.47 htmltools_0.5.8.1 rmarkdown_2.27 cli_3.6.2
[9] renv_1.0.7 compiler_4.4.1 rprojroot_2.0.4 here_1.0.1
[13] rstudioapi_0.16.0 tools_4.4.1 evaluate_0.24.0 Rcpp_1.0.12
[17] yaml_2.3.8 magick_2.8.3 rlang_1.1.4 jsonlite_1.8.8
Your practice R Project
Recall that we created a new R Project. It should now have:
data/ folderscripts/ (perhaps R scripts from last week, at least one Quarto script from this week)renv.lock file, .Rprofile, and a renv/ folderrenv package: creating a project-relative package library ✅Packagemanagement with {renv}