Creating and maintaining project-relative package libraries with renv
Humboldt-Universität zu Berlin
2024-06-11
Today we will…
renv
package to create and maintain a project-relative package libraryto read more on today’s topic, check out:
Ch. 10 (Basic reprodubility: freezing packages) from Rodrigues (2023)
the renv
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")
[1] '3.5.1'
Tools > Check for package updates
, orupdate.packages()
.libPaths()
renv
package to do thisrenv
renv
aids in maintaining r
eproducible env
ironments in R projectsrenv
freezes and stores package versions used in a projectrenv
renv
…
…can
…cannot
renv
workflowrenv
#| 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.lock
renv/
.Rprofile
renv.lock
R
: 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
renv
will just grab it from the global cachelme4
in other classes)
brms
] for Bayesian regression models using Staninstall_github()
function from either the remotes
or devtools
package (both are very common)renv::install()
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.0 (2024-04-24)
Platform: aarch64-apple-darwin20
Running under: macOS Ventura 13.2.1
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.44 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.0 rprojroot_2.0.4 here_1.0.1
[13] rstudioapi_0.16.0 tools_4.4.0 evaluate_0.23 Rcpp_1.0.12
[17] yaml_2.3.8 magick_2.8.3 rlang_1.1.4 jsonlite_1.8.8
Today we will…
renv
package to create and maintain a project-relative package library