R¶
R is an open-source language and environment for statistical computing and graphics.
Loading the module¶
module load R/4.4.1-gfbf-2023b
Pre-installed packages
In older R modules (R 4.2 and earlier), hundreds of CRAN and Bioconductor packages come bundled inside the R module itself. In newer versions (R 4.3+), R ships as a lighter module and the packages are provided separately via R-bundle-CRAN and R-bundle-Bioconductor (see below). Either way, check what is already available before installing your own:
installed.packages()[, "Package"]
Expand to see all available modules on each cluster
R/4.2.1-foss-2022a |
R/4.2.2-foss-2022b |
R/4.3.2-gfbf-2023a |
R/4.4.1-gfbf-2023a |
R/4.4.1-gfbf-2023b |
R/3.6.2-foss-2019b |
R/4.0.0-foss-2020a |
R/4.0.4-foss-2020b |
R/4.2.1-foss-2022a |
R/3.4.0-intel-2017a-X11-20170314 |
R/3.4.3-foss-2017b-X11-20171023 |
R/3.5.1-foss-2018b |
R/3.6.2-foss-2019b |
R/3.6.2-intel-2019a |
Batch job example¶
#!/bin/bash
#SBATCH --qos=regular
#SBATCH --job-name=r_job
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=4
#SBATCH --mem=32G
#SBATCH --time=08:00:00
#SBATCH --output=%x.%j.out
#SBATCH --error=%x.%j.err
module load R/4.4.1-gfbf-2023b
export OMP_NUM_THREADS=${SLURM_CPUS_PER_TASK}
Rscript my_analysis.R
You can also use R CMD BATCH my_analysis.R (output goes to my_analysis.Rout) or R < my_analysis.R --no-save. Prefer Rscript for new scripts; it is the simplest and most portable option.
Interactive sessions¶
To explore data or test code before submitting a batch job, request a compute node and start R interactively:
srun --qos=test --cpus-per-task=4 --mem=16G --time=01:00:00 --pty bash
module load R/4.4.1-gfbf-2023b
R
For a full IDE experience, see RStudio Server.
Pre-installed package bundles¶
In addition to R itself, we provide two package bundles from our central repository that cover most common use cases. Load them alongside R to get access to hundreds of packages without installing anything yourself:
module load R-bundle-CRAN
module load R-bundle-Bioconductor
The bundle must be compatible with your loaded R version and toolchain. Use module spider R-bundle-CRAN to see which versions are available and what R they require.
Highlights from R-bundle-CRAN
The CRAN bundle includes ~500 packages. Some of the most commonly used:
Data manipulation and tidyverse: dplyr, tidyr, readr, ggplot2, tibble, stringr, forcats, lubridate, tidyverse, data.table, readxl, haven, DBI, RSQLite, dbplyr
Statistics and modeling: lme4, glmnet, caret, survival, MASS, mgcv, nnet, randomForest, ranger, gbm, e1071, kernlab, pscl, quantreg, robustbase, mice, lavaan, brglm
Visualization: ggplot2, plotly, ggrepel, cowplot, patchwork, pheatmap, corrplot, ggpubr, GGally, ggridges, viridis, RColorBrewer, circlize, ComplexHeatmap
Spatial and geospatial: sf, terra, raster, sp, vegan, ape, geodist
Machine learning: caret, mlr, h2o, xgboost, Rtsne, umap, leiden, KODAMA
Parallel computing: parallel, foreach, doParallel, doMC, future, snow
Run installed.packages()[, "Package"] after loading the bundle to see the full list.
Highlights from R-bundle-Bioconductor
The Bioconductor bundle adds ~300 packages for genomics and bioinformatics:
RNA-seq: DESeq2, edgeR, limma, tximport, GenomicAlignments, Rsubread, ballgown
Single-cell: Seurat, scran, scater, SingleCellExperiment, sctransform, DropletUtils, scDblFinder
Genomic ranges and annotation: GenomicRanges, GenomicFeatures, biomaRt, AnnotationHub, ensembldb, org.Hs.eg.db, org.Mm.eg.db
Epigenetics: minfi, methylumi, bsseq, RnBeads, ChIPpeakAnno, ATACseqQC, chromVAR
Variant analysis: VariantAnnotation, SNPRelate, GENESIS, GWASTools
Pathway and enrichment: clusterProfiler, GSVA, fgsea, ReactomePA, GOstats, pathview
Flow/mass cytometry: flowCore, FlowSOM, CytoML, openCyto
Microbiome: phyloseq, dada2, mia, ANCOMBC, decontam
Installing R packages¶
DIPC staff installs commonly requested packages centrally. Contact support-hpc@dipc.org if you need something that is not available. You can also install packages locally in your own directory.
Setting up a local library¶
Add the following to your ~/.Renviron file (create it if it does not exist):
R_LIBS_USER=/scratch/%u/R/libs/%V
R expands %u to your username and %V to the R version. Then create the directory and install packages normally:
mkdir -p /scratch/$USER/R/libs/4.4.1
install.packages("ggplot2")
Use /scratch for local packages
Install packages under /scratch, not /home. The home directory has limited space (50 GB / 300K files) and packages with compiled code can be large. On Hyperion, /data is also an option if you want more permanent storage, but keep in mind that /data is read-only from compute nodes, so you must install all packages before submitting jobs.
Version mismatches
Packages compiled for one R version may not work with another. Use a version-specific library path (the %V placeholder handles this) and reinstall packages when you switch R versions.
Packages that fail to compile¶
Some packages need system libraries that are not available by default (e.g., sf and terra need GDAL, rstan needs a C++ toolchain). Before contacting support, check whether the library is already available as a module:
module spider GDAL
If a module exists, load it before running install.packages(). Make sure the module uses the same toolchain and version as your R module (e.g., if you loaded R/4.4.1-gfbf-2023b, look for a GDAL built with gfbf-2023b or compatible), and that it meets the minimum version the R package requires.
If the dependency is not available as a module or you are unsure which one to load, contact us at support-hpc@dipc.org with the error output; we can install the missing dependencies or the package itself as a system module.
Compiled packages on Hyperion¶
Hyperion has three CPU microarchitectures (CascadeLake, IceLake, Emerald Rapids). When you run install.packages() on a package that contains C, C++, or Fortran code, R compiles it for the CPU you are currently on. This means:
- A package compiled on a login node may crash with "illegal instruction" if your job lands on a node with a different CPU
- Or it may work everywhere but miss optimizations specific to newer CPUs
Two ways to handle this:
Simple approach -- add --constraint to your SLURM scripts so your jobs always run on the same architecture where you compiled. For example, if you installed packages from a login node with IceLake CPUs:
#SBATCH --constraint=icelake
Maximum compatibility -- compile inside an interactive job on the oldest architecture, so the binaries work everywhere:
salloc --qos=test --constraint=cascadelake --time=00:10:00 srun --pty bash
module load R/4.4.1-gfbf-2023b
R
install.packages("mypackage")
The second approach sacrifices some performance on newer nodes, but your packages will work on any node without worrying about constraints. For most R workloads the difference is negligible.
See Node selection for more details on microarchitecture constraints.
Parallel computing¶
R is single-threaded by default, but many operations (matrix algebra, apply on large data) can benefit from parallelism. There are two levels:
Implicit parallelism (BLAS)¶
Our R modules are compiled with OpenBLAS, which automatically parallelizes linear algebra operations (matrix multiplication, SVD, etc.) across multiple cores. This happens transparently; you do not need to change your code. The number of threads is controlled by OMP_NUM_THREADS, which the batch script examples above already set via SLURM_CPUS_PER_TASK.
Explicit parallelism (parallel package)¶
For embarrassingly parallel tasks (e.g., running the same analysis on many datasets), use the built-in parallel package:
library(parallel)
ncores <- as.integer(Sys.getenv("SLURM_CPUS_PER_TASK", "1"))
results <- mclapply(1:100, function(i) {
# your analysis here
expensive_function(i)
}, mc.cores = ncores)
mclapply uses forking and works within a single node. Match mc.cores to --cpus-per-task in your SLURM script. Other popular alternatives include foreach/doParallel for loop-style parallelism and future for a more flexible backend-agnostic approach; both are available in the CRAN bundle.
Memory with mclapply
Each forked process gets a copy of your R session's memory. If your session uses 8 GB and you fork 4 workers, you may need 32 GB total. Set --mem accordingly.
Memory tips¶
R loads all data into RAM. For large datasets:
- Use
object.size()to check how much memory an object uses, andgc()to trigger garbage collection after removing large objects - Consider
data.tableinstead ofdata.framefor large tabular data; it is significantly more memory-efficient and faster - If your data does not fit in memory, process it in chunks or consider a database backend (DBI + SQLite)
To estimate how much memory to request, run your script interactively on a small subset first, check peak memory with gc(), and extrapolate.
Troubleshooting¶
Warning: package was built under R version X.Y.Z
This means the package was compiled with a different R version than the one you are running. Reinstall the package with your current R version, or switch to the R version the package expects.
Locale warnings at startup
Messages like Setting LC_ALL to C or cannot set locale are harmless but noisy. You can silence them by adding to your ~/.bashrc:
export LC_ALL=C
Error in X11(): unable to open connection to display
R tries to open a graphical device by default. In batch jobs or non-graphical sessions, add this at the top of your script to use a file-based device instead:
options(bitmapType = "cairo")
pdf("plots.pdf") # or png(), svg(), etc.
Library path not detected / packages not found
Make sure your ~/.Renviron file contains the correct R_LIBS_USER path and that the directory exists. Check with:
.libPaths()
The first entry should point to your personal library under /scratch.
See also¶
-
Software catalog
Versions available on each cluster.
-
RStudio Server
Web-based R IDE on a compute node via SSH tunnel.
-
CRAN
The Comprehensive R Archive Network.