Passer au contenu principal

R on the clusters

R is provided via the DCSR software stack

Interactive mode

To load R:

module load gcc r
R
# Then you can use R interactively
> ...

Batch mode

While using R in batch mode, you have to use Rscript to launch your script. Here is an example of sbatch script, run_r.sh:

#!/bin/bash

#SBATCH --time 00-00:20:00
#SBATCH --nodes 1
#SBATCH --ntasks 1
#SBATCH --cpus-per-task 1
#SBATCH --mem 4G

module load gcc r

Rscript my_r_script.R

Then, just submit the job to Slurm:

sbatch run_r.sh

Package installation

A number of core packages are installed centrally - you can see what is available by using the library() function. Given the number of packages and multiple versions available other packages should be installed by the user. 

Installing R packages is pretty straightforward thanks to install.packages() function. However, be careful since it might fill your home directory very quickly. For big packages with large amount of dependencies, like adegenet for instance, you will probably reach the quota before the end of the installation. Here is a solution to mitigate that problem:

rm -rf $HOME/R
  • Create a new library in your scratch directory (obviously modify the path according to your situation):
mkdir -p /work/FAC/FBM/DEE/my_py/default/jdoe/R
cd $HOME
ln -s /work/FAC/FBM/DEE/my_py/default/jdoe/R
  • Install your R packages

Handling dependencies

Sometimes R packages depend on external libraries. For most of cases the library is already installed on the cluster you just need to load the module before trying to install the package from the R session.

If the installation of package is still failing you need to define the following variables. For example, if our package depend on gsl and mpfr libraries, we need to do the following:

module load gsl mpfr
export CPATH=$GSL_ROOT/include:$MPFR_ROOT/include
export LIBRARY_PATH=$GSL_ROOT/lib:$MPFR_ROOT/lib

Setting up an alternate personal library

If you want to set up an alternate location where to install R packages, you can proceed as follows:

mkdir -p ~/R/my_personal_lib2

# If you already have a ~/.Renviron file, make a backup
cp -iv ~/.Renviron ~/.Renviron_backup                  

echo 'R_LIBS_USER=~/R/my_personal_lib2' > ~/.Renviron

Then relaunch R. Packages will then be installed under ~/R/my_personal_lib2.