Skip to content

R

R is a programming language and environment for statistical computing and graphics, widely used in bioinformatics and data analysis. On our cluster, R is installed system-wide, so it is available without loading any module.

Checking R Version

R --version                 # Show installed R version

Tip

R is updated together with the operating system. After an update to a new minor version (e.g. from 4.6 to 4.7), packages from your personal library have to be reinstalled. Use renv or Conda if you need to keep one exact R version for a long-running project.

Running R Script

R scripts are run in batch jobs with the Rscript command.

Example content of my_script.R file:

data <- read.csv("my_data.csv")
model <- lm(y ~ x, data = data)
print(summary(model))
saveRDS(model, "my_model.rds")

Example

#!/bin/bash
#PBS -N r_job                               # Job name
#PBS -l select=1:ncpus=1:mem=8gb            # One core, 8 GB RAM
#PBS -l walltime=02:00:00                   # Max run time (hh:mm:ss)

cd ${PBS_O_WORKDIR}                         # Enter submission directory

module reset                                # Reset environment

Rscript my_script.R                         # Run your R script

This example runs my_script.R on one CPU core with 8 GB of memory and saves the fitted model into the my_model.rds file.

Installing Packages

Packages that are not included in the system R installation can be installed into your personal library in your home directory, without any help from the cluster administrators. Install packages on the login node or in an interactive job, not inside batch jobs, so that several jobs do not install the same package at the same time.

First, set up your personal library and default package repository. This is done only once and is stored in your home directory.

mkdir -p ${HOME}/R/library                                              # Create personal library
echo 'R_LIBS_USER=~/R/library/%v' >> ${HOME}/.Renviron                  # Library per R version
echo 'options(repos = c(CRAN = "https://cloud.r-project.org"))' >> ${HOME}/.Rprofile   # Default repository

The %v is replaced by the R version (e.g. 4.6), so packages for different R versions are kept separately. R uses the personal library only if the directory exists, so create it once for every new R version:

mkdir -p $(Rscript -e 'cat(Sys.getenv("R_LIBS_USER"))')

Now you can install packages:

R

install.packages("data.table")
library(data.table)

You can check where R looks for packages with:

.libPaths()

Note

Some packages are compiled from source during installation and need additional system libraries. If the installation fails with a missing library (e.g. libxml2, gdal, hdf5), find the library with module spider <name> and load it with module load before starting R. If the library is not available as a module, install the package using Conda, which provides R packages together with all required libraries (see Alternatives).

Bioconductor

Bioinformatics packages from Bioconductor are installed using the BiocManager package.

install.packages("BiocManager")
BiocManager::install(c("DESeq2", "edgeR"))

Reproducible Package Versions

For reproducible research, we recommend renv, which records the exact versions of all packages used in your project into a renv.lock file.

install.packages("renv")
renv::init()        # Create project-specific library
renv::snapshot()    # Record installed package versions
renv::restore()     # Reinstall recorded versions, e.g. on another system

Interactive R

For testing and interactive data analysis, run R inside an Interactive Job instead of on the login node.

qsub -I -l select=1:ncpus=4:mem=16gb -l walltime=02:00:00
R

Parallel Computing

By default, R uses a single CPU core. To use more cores, request them in your job and parallelize your code, for example with the built-in parallel package.

Warning

Functions such as parallel::detectCores() return the number of all cores of the node, not the number of cores allocated to your job. Always read the number of allocated cores from the NCPUS environment variable, otherwise your job will start more processes than it can use.

Example content of parallel.R file:

library(parallel)

ncores <- as.integer(Sys.getenv("NCPUS", "1"))      # Cores allocated by PBS

files <- list.files("mydata", pattern = "\\.csv$", full.names = TRUE)

results <- mclapply(files, function(f) {
  data <- read.csv(f)
  mean(data$value)
}, mc.cores = ncores)

saveRDS(results, "results.rds")

Example

#!/bin/bash
#PBS -N r_parallel                          # Job name
#PBS -l select=1:ncpus=16:mem=32gb          # One node, 16 cores, 32 GB RAM
#PBS -l walltime=04:00:00                   # Max run time (hh:mm:ss)

cd ${PBS_O_WORKDIR}                         # Enter submission directory

module reset                                # Reset environment

Rscript parallel.R                          # Run your R script

This example processes all CSV files in the mydata directory in parallel on 16 CPU cores.

Tip

Packages such as data.table and linear algebra libraries (BLAS) use multiple threads automatically. PBS limits them through OMP_NUM_THREADS to the requested number of cores. If you combine them with mclapply, set OMP_NUM_THREADS=1 in your job script to avoid starting too many threads.

Foreach

Code written with the foreach package can run in parallel using the doParallel backend.

library(doParallel)

ncores <- as.integer(Sys.getenv("NCPUS", "1"))
registerDoParallel(cores = ncores)

results <- foreach(i = 1:100, .combine = c) %dopar% {
  sqrt(i)
}

R With Job Arrays

Job Arrays are an easy way to run the same R script with many different inputs. Pass the array index to the script as an argument.

Example content of array.R file:

args <- commandArgs(trailingOnly = TRUE)
index <- as.integer(args[1])

data <- read.csv(sprintf("mydata/sample_%02d.csv", index))
write.csv(summary(data), sprintf("results/sample_%02d.csv", index))

Example

#!/bin/bash
#PBS -N r_array                             # Job name
#PBS -J 1-20                                # 20 array elements
#PBS -l select=1:ncpus=1:mem=4gb            # Each element: 1 core, 4 GB RAM
#PBS -l walltime=01:00:00                   # Max run time (hh:mm:ss)

cd ${PBS_O_WORKDIR}                         # Enter submission directory

module reset                                # Reset environment

Rscript array.R ${PBS_ARRAY_INDEX}          # Process one sample

This example runs 20 independent tasks, each processing one input file sample_01.csv to sample_20.csv.

Alternatives

If you need a different R version or a package that is difficult to install into the system R, you can set up R entirely in your home directory using:

  • Conda, which provides R and many R packages including their system libraries from the conda-forge and bioconda channels.
conda create -n r-env -c conda-forge -c bioconda r-base=4.4 r-data.table r-sf bioconductor-deseq2
conda activate r-env
Rscript my_script.R

Note

Do not combine the system R and a Conda R environment in one job. Run module reset before activating the Conda environment, check with which R that R from the environment is used, and install packages with conda install (e.g. r-<name> or bioconductor-<name>) instead of install.packages() where possible.

  • Singularity with ready-made R images from the Rocker project, e.g. singularity exec docker://rocker/tidyverse:latest Rscript my_script.R.