# R on the clusters (old)

R is provided via the [DCSR software stack](https://wiki.unil.ch/ci/books/high-performance-computing-hpc/page/dcsr-software-stack)

### Interactive mode

To load R:

```shell
module load 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`:

```shell
#!/bin/bash

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

module load r

Rscript my_r_script.R
```

Then, just submit the job to Slurm:

```shell
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()](https://stat.ethz.ch/R-manual/R-devel/library/utils/html/install.packages.html) 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:

- Remove your current R library (or set up an alternate one as explained in the section [Setting up an alternate personal library](#bkmrk-setting-up-an-altern) below):

```shell
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
```

- Create a symlink to locate the R library on the scratch dir:

```shell
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:

```bash
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`.