Skip to content

Instantly share code, notes, and snippets.

@eveyp
Last active September 5, 2022 05:53
Show Gist options
  • Save eveyp/07b54986a2218d9ebb6085bb84f04cd1 to your computer and use it in GitHub Desktop.
Save eveyp/07b54986a2218d9ebb6085bb84f04cd1 to your computer and use it in GitHub Desktop.
renv and Singularity Containers

Using renv in Singularity Containers

The Problem

My HPC runs CentOS while the container is running Ubuntu Focal. This means that when packages link a system dependency, that system dependency might be in different places. So, I can't use the same built binaries for some packages because they'll break when a system dependency they need isn't where it is expected (this happens when a package is built in one OS but loaded in the other).

The Solution

To fix this I need to keep the CentOS and Ubuntu libraries separate from each other. This is simple enough when not using renv, but I'm looking for reproducibility, so I need an renv compatible way to keep the libraries separate.

To do this I define an Renviron.site file that will be used by the container that sets an environmental variables for a prefix for the renvcache and the project library. Then I bind mount the Renviron.site file in the container and renv picks up the environmental variable and keeps things separate.

Renviron.site

Here's the relevant portion of the Renviron.site file:

RENV_PATHS_PREFIX=rocker

Now when I'm working in the container renv uses ~/.cache/R/renv/cache/v5/rocker/R-4.1/x86_64-pc-linux-gnu for the cache instead of ~/.cache/R/renv/cache/v5/R-4.1/x86_64-pc-linux-gnu and renv/library/rocker/R-4.1/x86_64-pc-linux-gnu for the project library instead of renv/library/R-4.1/x86_64-pc-linux-gnu. If I'm working directly in the HPC (including submitting jobs), renv uses the latter set of paths so the CentOS versions of the packages are loaded.

Bind Mount

When running singularity exec I add this option to bind mount the Renviron.site file into the container:

--bind ~/.Renviron.site-rocker:/usr/local/lib/R/etc/Renviron.site
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment