R is a system for statistical computation and graphics. It consists of a language plus a run-time environment with graphics, a debugger, access to certain system functions, and the ability to run programs stored in script files.
Even though R was not developed for high performance computing (HPC), its popularity with scientists from a variety of disciplines, including engineering, mathematics, statistics, bioinformatics, etc. makes it an essential tool on HPC installations dedicated to academic research. Features such as C extensions, byte-compiled code and parallelisation allow for reasonable performance in single-node jobs. Thanks to R’s modular nature, users can customize the R functions available to them by installing packages from the Comprehensive R Archive Network (CRAN) into their home directories.
The R interpreter
You need to begin by loading an R module; there will typically be several versions available and you can see a list of all of them using the command
[name@server $] module spider r
You can load a particular R module using a command like
[name@server $] module load r/3.3.3
and with R now loaded in your environment, you can start the R interpreter and type R code inside that environment:
[name@server $] R R version 3.3.3 (2017-03-06) -- "Another Canoe" Copyright (C) 2017 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > values <- c(3,5,7,9) > values  3 > q()
To execute R scripts, use the Rscript front-end with the file containing the R commands as an argument:
[name@server $] Rscript computation.R
This front-end will automatically pass scripting-appropriate options --slave and --no-restore to the R interpreter. These also imply the --no-save option, preventing the creation of useless workspace files on exit.
Installing R packages
To install packages from CRAN, you can use the install.packages facility inside the R interpreter. For example, to install the sp package that provides classes and methods for spatial data, use the following command on a login node:
[name@server $] R [...] > install.packages("sp")
When asked, select an appropriate mirror for download. Ideally, it will be geographically close to the cluster you're working on.
Some packages require defining the environment variable TMPDIR before installing and others expect the Gnu C and/or C++ compiler to be used rather than the Intel compiler family, so it is prudent to first load one of the gcc modules before starting R.
To install a package that you downloaded (i.e. not from CRAN), you can install it as follow. Assuming the package is named archive_package.tgz, run the following command in a shell:
[name@server $] R CMD INSTALL -l 'path for your local (home) R library' archive_package.tgz
This next procedure installs Rmpi, an interface (wrapper) to MPIMessage Passing Interface routines, which allow R to run in parallel.
1. See the available R modules by running:
module spider r
2. Select the version (here, we indicate the latest Novembre 2017 version), and also load the required OpenMPI module:
module load r/3.4.0 module load openmpi/1.10.7
3. Download the latest R version; change the version number to whatever is desired.
4. Specify the directory where you want to install the package files; you must have write permission for this directory. The directory name can be changed if desired.
mkdir -p ~/local/R_libs/ export R_LIBS=~/local/R_libs/
5. Run the install command.
R CMD INSTALL --configure-args="--with-Rmpi-include=$EBROOTOPENMPI/include --with-Rmpi-libpath=$EBROOTOPENMPI/lib --with-Rmpi-type='OPENMPI' " Rmpi_0.6-6.tar.gz
This uses OpenMPI version 1.10.7 which is needed to spawn processes correctly (the default MPIMessage Passing Interface module 2.1.1 has a problem with that at present).
1. Place your R code in a script file, in this case the file is called test.R.
#Tell all slaves to return a message identifying themselves. library("Rmpi") sprintf("TEST mpi.universe.size() = %i", mpi.universe.size()) ns <- mpi.universe.size() - 1 sprintf("TEST attempt to spawn %i slaves", ns) mpi.spawn.Rslaves(nslaves=ns) mpi.remote.exec(paste("I am",mpi.comm.rank(),"of",mpi.comm.size())) mpi.remote.exec(paste(mpi.comm.get.parent())) #Send execution commands to the slaves x<-5 #These would all be pretty correlated one would think x<-mpi.remote.exec(rnorm,x) length(x) x
2. Copy the following content in a job submission script called job.sh:
#!/bin/bash #SBATCH --account=def-ppomorsk # replace this with your own account #SBATCH --ntasks=5 # number of MPI processes #SBATCH --mem-per-cpu=2048M # memory; default unit is megabytes #SBATCH --time=0-00:15 # time (DD-HH:MM) module load r/3.4.0 module load openmpi/1.10.7 export R_LIBS=~/local/R_libs/ mpirun -np 1 R CMD BATCH test.R test.txt
3. Submit the job with:
For more on submitting jobs, see the Running jobs page.