Repository navigation
Using renv for Reproducible R Environments
Who this is for: anyone working in a lab project, which all use renv. Before you start: Getting Started with EpiModelHIV walks you through your first project setup; this page explains what renv is doing and how it works on the HPC.
Different projects need different versions of the same R packages (for example, two branches of EpiModelHIV-p). The renv package gives each project its own package library and records the exact versions in a lockfile, so that you, your collaborators, and the HPC all run the same code.
-
renv.lock: every package and version the project uses on your computer (models plus analysis packages). -
renv.lock.hpc: a smaller lockfile with only what the simulations need on the HPC (EpiModelHIV, EpiModelHPC, ARTnetData, and their dependencies).make_em_workflow()inR/hpc_configs.Rregenerates it from your local library each time you build a workflow, so commit and push it before every HPC run. -
The project library (
renv/library/): where the packages are installed. Not committed to git. -
The cache: renv keeps one copy of each package version per user (on the HPC in
~/.cache/R/renv) and links it into each project, so a second project with the same versions installs in seconds.
In our projects (all created from the EpiModelHIV-Template) git tracks .Rprofile, renv.lock, and renv.lock.hpc. The whole renv/ folder and .Renviron are ignored. Our .Rprofile is lab code: it activates renv when renv/activate.R exists, and otherwise prints a message telling you how to set up the project. Do not add .Rprofile to .gitignore. (renv's own default is to also commit renv/activate.R and renv/settings.json; we regenerate them instead.)
Install renv once with install.packages("renv"). Then, in a new project created from the Template, follow R/00-setup.R:
- Run
renv::init()and choose 1: Restore the project from the lockfile. (In a non-interactive sessionrenv::init()does not install anything; userenv::init(bare = TRUE)followed byrenv::restore()instead.) - Restart R.
- Install packages with
renv::install()(for examplerenv::install("EpiModel/EpiModelHIV-p@<branch>")), record them withrenv::snapshot(), and commit the updatedrenv.lock.
If renv::init() fails partway (for example on a download error), fix the cause, restart R, and run renv::init() again with option 1. Never run renv::restore() by itself while renv/activate.R is missing: the project is not active yet, so renv installs into your personal R library instead of the project's.
After you git pull changes from a collaborator, restart R. The Template's .Rprofile runs renv::status() at startup; if it reports differences, run renv::restore().
EpiModelHIV-p and ARTnetData are private repositories, so renv needs your GitHub personal access token to install them. Put it in the project's .Renviron (see Getting Started with EpiModelHIV); a project .Renviron hides your user ~/.Renviron, so a token stored only there is not seen inside the project.
You usually do not run renv by hand on the HPC. The first step of every workflow (step_tmpl_renv_restore(), added by make_em_workflow()) runs git pull and then renv::restore(lockfile = "renv.lock.hpc") on a compute node. See Running Project Workflows on the HPC.
One-time setup on the HPC:
- Clone the project over SSH into your project folder:
cd /projects/epimodel/<netid>/thengit clone git@github.com:EpiModel/<Project>.git. Do not put a token in the clone URL; git would store it in plain text in.git/config. - Put your
GITHUB_PAT=<your token>line in~/.Renvironon the HPC and make it private withchmod 600 ~/.Renviron(see Using Git on the HPC). Project.Renvironfiles are not committed, so on the HPC R reads your user~/.Renviron.
To restore by hand (for example before an interactive session), use a batch job run from the project root:
#!/bin/bash
#SBATCH --job-name=renv-restore
#SBATCH --partition=epimodel
#SBATCH --cpus-per-task=4
#SBATCH --mem=16G
#SBATCH --time=01:00:00
#SBATCH --output=renv-restore_%j.out
cd /projects/epimodel/<netid>/<Project>
. /projects/epimodel/spack/share/spack/setup-env.sh
spack unload -a
spack load r@4.5.1 # the R version in your project's R/hpc_configs.R
Rscript -e "renv::init(bare = TRUE, load = FALSE)"
Rscript -e "renv::restore(lockfile = 'renv.lock.hpc')"A first restore of the Template's renv.lock.hpc (about 80 packages built from source) takes about 5 minutes on 4 CPUs; later restores reuse the cache and take seconds.
Two rules:
-
Start R from the project root. R reads
.Rprofilefrom the folder it starts in, and that is what activates the project library. From any other folder you get your personal library instead. -
Restore on a compute node, never on the login node. The login node runs Rocky Linux 8.9 and the compute nodes 8.10, and renv keeps a separate library for each (
renv/library/linux-rocky-8.9/andrenv/library/linux-rocky-8.10/). Packages installed on the login node are not the ones your jobs load.
- A startup message telling you to run
renv::init(bare = TRUE)orR/00-setup.R: therenv/folder does not exist yet. This is normal right after cloning. -
renv 1.3.1 was loaded from project library, but this project is configured to use renv 1.2.3(or similar version numbers): your renv is newer than the one recorded inrenv.lock. Harmless. -
GitHub authentication credentials are not available, thenerror downloading 'https://api.github.com/repos/EpiModel/EpiModelHIV-p/tarball/...'with[error code 22]or[error code 56], followed byRolled back installation: R cannot see a validGITHUB_PAT. The token is missing, expired, lacks access to the private repositories, or (on your computer) is in~/.Renvironbut not in the project.Renviron. Fix the token, restart R, and run the restore again; the packages already built are in the cache. - An HTTP
401error when installing from GitHub: the token has expired or been revoked. Make a new one.
More detail is on the renv website.
EpiModel Research Lab wiki. Something out of date or unclear? Post in #coding-help on the lab Slack, or fix it yourself: see Contributing to This Wiki.
0. Start here
1. Your computer
2. Learn EpiModel
3. First project
4. Model code
5. HPC
- Access and login
- Environment setup
- Rules, storage, and help
- Files
- Git
- R via Spack
- srun
- sbatch
- Monitoring jobs
- Project workflows
- Checkpoints
6. Calibrate and analyze
7. Publish
8. Admin