20:00
~20 min
?renv::init
, ?renv::status
, ?renv::snapshot
, and ?renv::restore
{renv}
Package: a container for functions and data
Library: a folder on a computer in which installed packages are stored
Repository: a source of packages (often on Internet)
Reminder
Functions and data are identified by their names (which cannot be duplicate within the same package).
When you library()
a package you attach it to the current R session.
Attaching a package to the R session, it means to make it function and data names available for you to use. I.e., you are (within the session) expanded the language!
The most common repository is CRAN (The Comprehensive R Archive Network) from which you can install packages from any R session by install.packages()
.
Other repositories are:
System: shared across all users and projects
User: shared by all the user projects
Project: powered by renv, is the project its own independent collection of packages
Tip
Call .libPaths()
to know which library are you using in the session you are.
{renv}
[side]To create a project powered by renv you can simply tick the corresponding option in RStudio at creation time.
{renv}
main workflowinit()
: setup the renv infrastructureinstall()
/update()
/install.packages()
: install/update packagessnapshot()
: update the renv.lock
file writing metadata about the current state of the project libraryrestore()
: restore the library accordingly to the what is prescribed in the renv.lock
file
status()
: check for differences between therenv.lock
file and the packages installed in the project library.
{renv}
To convert an existing project to use renv call renv::init()
.
This creates:
A renv/library
folder that will be the projects library containing all the packages used within the project
renv.lock
file which is the current package inventory of your projects, storing metadata of project used packages so that anyone can re-install them all (with exact the same version) on any other machine.
A project dedicated .Rprofile
; which is an R script that is automatically run at every R start, just before to giving you the power to interact with the R session, and it is used by renv to configure the project library in the current session.
ME: Connect to the course-scripts project in RStudio cloud (https://bit.ly/ubep-rws-rstudio): script
06-renv.R
{renv}
cache [optional]Every renv projects starts with an empty library (there is only the renv package itself).
Working on many projects you will probably use same packages on different projects, so you will need to install them multiple times!
Important
Installing a package means:
every time…
That is managed efficiently by a renv global cache, which permit to download and install a specific package (with a specific version) only once, so that installing the same package on multiple projects takes time only the first time, and it will be lighting fast in all the subsequent ones.
20:00
Connect to our pad (https://bit.ly/ubep-rws-pad-ed3) (Ex. 6)
Connect to Day 2 project on our RStudio cloud (https://bit.ly/ubep-rws-rstudio)
Your turn
renv.lock
fileR/
, analyses/
, and output/
R/functions.R
and define a function make_plot()
returning a ggplot2
plot of mtcars
analyses/analysis.R
that:ggplot2
packagehere
packagefunctions.R
scriptmake_plot()
functionoutput/
folderanalyses/analysis.R
script.status()
: check differences between renv.lock
and packages installed in the project’s library.
YOU: Connect to our pad (https://bit.ly/ubep-rws-pad-ed3) and write there questions & doubts (and if I am too slow or too fast)
To create the current lesson, we explored, used, and adapted content from the following resources:
{renv}
website.
The slides are made using Posit’s Quarto open-source scientific and technical publishing system powered in R by Yihui Xie’s Kintr.
This work by Corrado Lanera, Ileana Baldi, and Dario Gregori is licensed under CC BY 4.0
UBEP’s R training for supervisors