R & RStudio

Table of contents

  1. R modules
    1. Installing R packages
    2. Using R in batch jobs
  2. RStudio
  3. R with conda

R modules

These R versions are a beta feature that requires enabling Lmod.

Ada provides R 4.3, 4.4, 4.5 and 4.6 as modules. To list them and load one:

[ada ~]$ module spider r
...
    Versions:
        r/4.3.3-hoj4as6
        r/4.4.3-4zwq6dj
        r/4.5.3-cmed6hu
        r/4.6.1-b4td7il
...
[ada ~]$ module load r/4.6.1

The module names are automatically created with a short identifier (e.g., “hoj4as6”) to differentiate compiler settings and maintain provenance. You can load modules by their full name (with the identifier) or just the name and version (e.g., module load r/4.6.1).

Installing R packages

Start R and use install.packages() as usual, e.g., install.packages("tidyverse"). Packages are installed into a personal library in your home directory (under ~/R/), separate for each R version (e.g., 4.5 vs. 4.6), and are downloaded from CRAN without prompting you to pick a mirror. These R installations include the libraries needed by most common packages, such as graphics (Cairo, PNG, JPEG, TIFF, Tcl/Tk) and Java (e.g., for rJava).

Using R in batch jobs

Load the R module in your job script and run your script with Rscript:

#!/usr/bin/env bash
#SBATCH --job-name=r_analysis
#SBATCH --partition=standard
#SBATCH --time=04:00:00
#SBATCH --mem=8G

module load r/4.6.1
Rscript my_analysis.R

RStudio

The RStudio application is a beta feature of Open OnDemand.

You can run the RStudio IDE on a compute node through Open OnDemand. Log in to ood.middlebury.edu, select Interactive Apps > Rstudio, and fill in the form. In addition to the common fields (node type, hours, cores, memory), choose the R Version (4.3, 4.4, 4.5 or 4.6). Click Launch, and once the session is running, Connect to RStudio Server. Remember to Delete the session when you are finished.

RStudio uses the same R installations as the R modules. Packages you install in RStudio are available to batch jobs that load the same R version, and vice versa. You don’t need to enable Lmod to use RStudio.

R with conda

You can also use the conda package manager to work with R (see Python & Jupyter for an introduction to conda). Conda can be particularly useful for R packages that depend on native libraries not otherwise available on Ada. You may need the conda-forge repository in addition to the default repositories.

Create a new conda environment for your project with the r-base and r-essentials packages. Here we create an environment named my_r_env with the latest R version (specify a version of r-base to choose a different version):

conda create --name my_r_env r-base r-essentials
conda activate my_r_env

With the environment activated you can install R packages provided through conda repositories (typically prefixed with “r-“, e.g., r-ncdf4) or by starting R and using install.packages(). With the environment activated, R is provided by the environment (separate from the R modules or other environments). Whenever you want to use the R packages associated with an environment, activate that environment first. SLURM jobs inherit the current environment, so they will use your activated conda environment.