Skip to content

R on the cluster ​

R supports statistical analysis, data processing, and visualization. Load the platform's R module after allocating compute resources.

Compute on allocated nodes

Use login nodes only for editing and brief checks. Submit sustained, batch, CPU-intensive, or memory-intensive analyses through Slurm.

Interactive R ​

Request one node, eight CPUs, 16 GB of memory, and five minutes:

bash
salloc -N 1 --cpus-per-task=8 --mem=16G -t 00:05:00 -p qcpu_18i

Example allocation output:

text
salloc: Pending job allocation 273377
salloc: job 273377 queued and waiting for resources
salloc: job 273377 has been allocated resources
salloc: Granted job allocation 273377
salloc: Nodes bnode1 are ready for job

Connect to the node actually assigned to your job (the example uses bnode1):

bash
ssh bnode1

Load R and start it:

bash
module load R
bash
R

Test:

r
print(1 + 2)

Expected output:

text
[1] 3

Batch analysis ​

Save the analysis as analysis.R:

r
args <- commandArgs(trailingOnly = TRUE)
input_file <- args[1]
output_file <- args[2]

data <- read.csv(input_file)
summary_result <- summary(data)

sink(output_file)
print(summary_result)
sink()

Save this Slurm script as run_r.slurm:

bash
#!/bin/bash
#SBATCH --job-name=r-analysis
#SBATCH --partition=qcpu_18i
#SBATCH --nodes=1
#SBATCH --cpus-per-task=8
#SBATCH --mem=16G
#SBATCH --time=02:00:00
#SBATCH --output=%x_%j.out
#SBATCH --error=%x_%j.err

set -euo pipefail

module purge
module load R

Rscript analysis.R input.csv result.txt

Submit and monitor:

bash
sbatch run_r.slurm
bash
squeue -u $USER

Personal packages ​

Install packages into a directory you own:

r
dir.create("~/R/library", recursive = TRUE, showWarnings = FALSE)
.libPaths(c("~/R/library", .libPaths()))
install.packages("ggplot2", lib = "~/R/library")

Add that library path in later scripts:

r
.libPaths(c("~/R/library", .libPaths()))
library(ggplot2)

If CRAN is unreachable, ask for help configuring a mirror or upload packages for offline installation.

Demonstration ​

Interface illustration

Troubleshooting ​

If R is missing, inspect module list, which R, and which Rscript. Package installation failures commonly involve network access, compiler dependencies, or directory permissions. Use your personal library and read the error messages. Allocating several CPUs does not automatically parallelize R: your code or packages must explicitly support parallel execution.

Copyright 2021 Biodesign Center. All rights reserved.