R Packages with renv

You may use the renv R package to create a personal R Project environment for R packages. Documentation on renv can be found on the RStudio site.

Setup

Say your R code is in directory /scratch/$USER/projects/project1

cd /scratch/$USER/projects/project1
module purge
module load r/gcc/4.1.2

R

Automatic deletion of your files

This page describes the installation of packages on /scratch. One has to remember, though, that files stored in the HPC scratch file system are subject to the HPC Scratch old file purging policy: Files on the /scratch file system that have not been accessed for 60 or more days will be purged (read more).

Thus you can consider the following options

Cache directory setup

By default, renv will cache package installation files to your home directory (most likely either in ~/.local/share/renv or ~/.cache/R/renv/ or something similar). 

To avoid filling up your home directory, we advise to set up path to alternative cache directory (otherwise your home directory may fill up quickly)

   mkdir -p /scratch/$USER/.cache/R/renv
RENV_PATHS_ROOT=/scratch/<USER_NETID>/.cache/R/renv

Init renv

The renv package is already installed for module r/gcc/4.1.2. You need to install it yourself if you use other R module version

## Do this if renv is not available (already installed for r/gcc/4.1.2)# install.packages("renv")  ## By default this will install renv package into a sub-directory within your home directory
## init renv in project's directoryrenv::init(".")
R version 4.1.2 (2021-11-01) -- "Bird Hippie"...
* Project '/scratch/$USER/projects/project1' loaded. [renv 0.14.0]

Check

> .libPaths()[1] "/scratch/$USER/projects/project1/renv/library/R-4.1/x86_64-pc-linux-gnu"
renv::paths$cache()#[1] "/home/$USER/.cache/R/renv/cache/v5/R-4.1/x86_64-pc-linux-gnu"

Add/remove, etc. packages

Install a package, such as reshape2. Below we can see it is not yet installed and then install it.

R
library(reshape2)

Error in library(reshape2) : there is no package called ‘reshape2install.packages("reshape2")

Note: you must be in the project1 directory for renv to load your project and the appropriate personal environment that you have created. If you want to copy your environment to a new location, use the bundle package, as shown below.

Test R file

print("hello")renv::restore()library(reshape2)names(airquality) <- tolower(names(airquality))head(airquality)aql <- melt(airquality)print("hello again")

For testing run it as

srun --pty /bin/bash
Rscript test.R

Note: your '.Rprofile' file will include line source("renv/activate.R")

The file will output the following:

[1] "hello"* The library is already synchronized with the lockfile.  ozone solar.r wind temp month day1    41     190  7.4   67     5   12    36     118  8.0   72     5   23    12     149 12.6   74     5   34    18     313 11.5   62     5   45    NA      NA 14.3   56     5   56    28      NA 14.9   66     5   6No id variables; using all as measure variables[1] "hello again"

Clean up

Keep only the packages that you use in this particular project (not all the packages available on the system)

R # launch Rrenv::clean() # remove packages not recorded in the lockfile from the target library

Recommended Workflow

The general workflow when working with renv is:

The renv::init() function attempts to ensure the newly-created project library includes all R packages currently used by the project. It does this by crawling R files within the project for dependencies with the renv::dependencies() function. The discovered packages are then installed into the project library with the renv::hydrate() function, which will also attempt to save time by copying packages from your user library (rather than reinstalling from CRAN) as appropriate.

Calling renv::init() will also write out the infrastructure necessary to automatically load and use the private library for new R sessions launched from the project root directory. This is accomplished by creating (or amending) a project-local .Rprofile with the necessary code to load the project when the R session is started.

If you’d like to initialize a project without attempting dependency discovery and installation – that is, you’d prefer to manually install the packages your project requires on your own – you can use renv::init(bare = TRUE) to initialize a project with an empty project library.

Use with sbatch

When you launch a job with sbatch,  R will check if there is renv directory, and if renv is on it will pick up packages, installed using renv in the current directory.

Before you launch sbatch job, you need to make sure your project renv environment is ready, as outlined in the previous section.

Store and Share your R Project's R version and R Package Versions

Reproduce Environment

If you already have file renv.lock or bundle file skip step 1

1. In the original location (your own laptop for example) go to project directory and execute

(Make sure the whole path to project directory and names of your script files don't have empty spaces!)

R# install.packages("renv")  ## if neededrenv::init()renv::snapshot()

2. Take file renv.lock and copy it to a new location for the project

3. At the new location - restore environment: go to directory of the project and execute. (Make sure version of R is the same)

## Reproduce environment
module purge
module load r/gcc/4.1.2
R
renv::restore()
renv::init()

renv will install/compile what is needed on any system (Linux, Windows, etc). You can share your code with other researchers no matter what system they use. However, you should be careful that the same version of R is used between systems.

What to save/publish/commit with Git

In order to have your work reproducible by you or/and others, save and/or commit your code in git, please including

Migrating from Packrat

The renv package has replaced the now deprecated Packrat package. The renv::migrate() function makes it possible to migrate projects from Packrat to renv. See the ?migrate documentation for more details. In essence, calling renv::migrate("<project path>") will be enough to migrate the Packrat library and lockfile such that they can then be used by renv.

Useful links