R

R is a programming language and software environment for statistical computing, data analysis, and visualization.

Usage

To access R on MOGON, check for available R modules and load the version you need.

module avail lang/R/
module load lang/R/

To check the R version and the location of the executable, use:

R --version
which R

You can start an R session for interactive testing or checking and installing available packages. Start R with:

R

You can then execute R commands directly in the console. To leave the session, type q(). You can save the workspace image if desired. For longer computations, submit an R script as a batch job using Slurm.

See the official R documentation for further information on R directy.

Using R Libraries

If you need a specific library, you need to check if the package is available in your R session and install it if it is missing. To list all packages installed for the currently loaded R module version, run:

print(as.data.frame(installed.packages()[, c(1, 3:4)]))

To list only user-installed packages, run:

ip <- as.data.frame(installed.packages()[, c(1, 3:4)])
rownames(ip) <- NULL
ip <- ip[is.na(ip$Priority), 1:2, drop = FALSE]
print(ip)

Missing R packages can be installed using install.packages(). For example:

install.packages(c("package_A", "package_B"))

R will ask you to select a directory for the installation. You can choose a directory in your home directory. When using packages in a batch job, make sure they are available for the R version loaded in the job environment.

Please contact the HPC team if a package requires system-wide dependencies, is large or would be useful to a wider range of users.

Not all CRAN mirrors are supported. Please select the mirror in Erlangen, Germany.

Submitting Jobs: Things to Consider

R scripts can be executed non-interactively using the R interpreter. You can run an R script with the following command:

R --no-save -f <path/to/R_script.R>

where:

  • -f: specifies the file containing the R commands to execute.
  • --no-save: R workspace is not saved on exit. This prevents name space clashes, if you have used R previously in different scenarios.

Additional command line options are available. After loading the module, you can see a list of all available flags with the following command:

R --help

For longer-running or resource-intensive computations, you should submit your R script as a batch job. To do so, create a Slurm job script that loads the R module and executes your R script, then submit it using sbatch.

Simple Serial Example

Create a file named hello_mogon.R:

print("Hello MOGON!")

Create a Slurm job script named serial_R_job.slurm in the same directory:

#!/bin/bash
#SBATCH --account=<ki-project>
#SBATCH --job-name=R_serial_example
#SBATCH --partition=ki-smallcpu  

#SBATCH --time=0-00:01:00
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1

#SBATCH --output=%x_%j.out
#SBATCH --error=%x_%j.err

#========[ + + + + Environment + + + + ]========#
# Load all necessary modules to ensure a consistent environment.
module purge
module load lang/R/4.5.2-gfbf-2024a


#========[ + + + + Job Steps + + + + ]========#
srun R --no-save -f hello_mogon.R

Submit the job with:

sbatch serial_R_job.slurm

After the job has finished, inspect the output:

cat R_serial_example_<job-id>.out

The output should contain:

[1] "Hello MOGON!"

The job’s standard error is written to the corresponding .err file. Check both output files if the job fails.

See our MOGON job submission documentation for further information about Slurm jobs and resource requests.

Compiling Functions

As your script gets more complex, compiling self-declared functions with the compiler package is recommended, as it may improve performance for computationally intensive code. Benchmark your code to determine whether compilation helps.

Use case example:

library(compiler)

f <- function(n, x) for (i in 1:n) x <- (1 + x)^(-1)
g <- cmpfun(f)

Parallel Computing with R

R offers several packages for parallel or HP-computing. Parallel computing is supported through packages such as parallel, which is included with R. Additional packages that support MPI-based execution parallelism are discussed below.

See the parallel documentation for more information about the package.

Multithreading and OpenBLAS

Some R operations rely on numerical libraries like OpenBLAS, which may use multiple threads. OpenBLAS uses OpenMP for multithreading, so the number of threads must be set with:

export OMP_NUM_THREADS = <num_threads>

Please note, that this setting is only relevant to software that uses OpenMP. It does not necessarily control every threading mechanism used by R packages or numerical libraries. Check the documentation of the library you want to use. Furtherore, the number of threads should be consistent with the CPU resources allocated to the job.

MPI with R

Packages for parallel execution using multiple processes or distributed-memory computing are provided by R. Rmpi, doMPI and snow probably are the most common packages for that purpose in conjunction with MPI. For further informtion on how to use these libraries, refer to the documentation linked above (the snow link contains a bunch of examples).

Packages that rely on MPI may require compatible MPI modules to be loaded before execution and installation. Check for the OpenMPI module available on MOGON and load it.

module avail OpenMPI

You can install the required packages using the following command. See the instructions above for details on the installation process.

install.packages(c("snow", "Rmpi", "doMPI"))

Bioconductor

Bioconductor provides packages for the analysis and comprehension of high-throughput biological data, including genomic and transcriptomic data.

It is commonly used with R in bioinformatics workflows.

Before using Bioconductor on MOGON, check which packages are available in the selected R environment and whether additional installation steps are required. Package compatibility can depend on the R version.

See the Bioconductor documentation for installation instructions and further information.