Running R

Versions of R

Multiple versions of R are available on Kamiak via modules.  Simply type module load r to load the latest version or use module avail r to find an older version.

To avoid problems, be sure to install all libraries under the same version of R, since R often does not preserve backwards compatibility.

Locally Installing R Packages

The installation of R on Kamiak includes base packages, but not many others.  Additional packages can be installed by users in their /home directory or to other storage they have access to.  Labs may wish to store their packages collectively, in a directory under /data/YourLabName.  This allows users or labs to create their own package environments which can they can change or update as needed.

To locally install R packages, type the following commands to append the required setup to your .bashrc, and then exit and re-login to make that change effective:

mkdir -pv ~/R/lib
echo 'export R_LIBS_USER=~/R/lib' >> ~/.bashrc
exit

Below is an example of installing an R package to a home directory:

  1. Load your chosen version of R:
    [~]$ module load r # Use r/X.X.X for a specific version
  2. Start R and begin installing packages of your choice. Here is an example for “dplyr” package:
    [~]$ R
    R version 3.3.0 (2016-05-03) -- "Supposedly Educational"
    Copyright (C) 2016 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
    >
    > install.packages("dplyr", dependencies=TRUE, repos="http://cran.r-project.org")
    > # To install the development version of dplyr from GitHub, do the following
    > install.packages("devtools", dependencies=TRUE, repos="http://cran.r-project.org")
    > library(devtools)
    > install_github("hadley/dplyr")
    > # To install an older version of the package
    > library(devtools)
    > install_version("dplyr", version = "0.4.3", repos = "http://cran.us.r-project.org")

Some R packages will have dependencies which you will need to provide in order to compile and install them.  These may already be available in Kamiak in other modules or you will need to download, compile, and install the dependencies just as you would do on Kamiak when building any software outside of R.  In some cases you may need to provide additional options such as to specify where library and include files may be.  What options are available depends on the package being compiled, for example:

> install.packages('rgdal', configure.args=c('--with-proj-include=/opt/apps/proj/4.9.2/include','--with-proj-lib=/opt/apps/proj/4.9.2/lib'))

Installing devtools in R

The library “devtools” can be a bit tricky to install.
Below are the steps to install it.

module load r # Use r/X.X.X for a specific version
export LD_LIBRARY_PATH=/opt/apps/common/libgit2/1.7.1/lib64:/opt/apps/common/freetype2/2.13.2/lib:/opt/apps/common/fribidi/1.0.13/lib:/opt/apps/common/libpng/1.6.40/lib:$LD_LIBRARY_PATH
export LIBRARY_PATH=/opt/apps/common/libgit2/1.7.1/lib64:/opt/apps/common/freetype2/2.13.2/lib:/opt/apps/common/fribidi/1.0.13/lib:/opt/apps/common/libpng/1.6.40/lib:$LIBRARY_PATH
export CPATH=/opt/apps/common/libgit2/1.7.1/include:/opt/apps/common/freetype2/2.13.2/include/freetype2:/opt/apps/common/fribidi/1.0.13/include/fribidi:/opt/apps/common/libpng/1.6.40/include:$CPATH
export PKG_CONFIG_PATH=/opt/apps/common/fribidi/1.0.13/lib/pkgconfig:$PKG_CONFIG_PATH
R
Sys.setenv(USE_SYSTEM_LIBGIT2=1)
install.packages('devtools', repos = 'https://cran.rstudio.com', dependencies = TRUE)

To use the library once installed:

module load r # Use r/X.X.X for a specific version
R
library("devtools")

Using devtools in the Kamiak shared site library

Instead of locally installing devtools and its dependencies, a pre-compiled version is available for the latest version of R in the shared site library in /opt/apps/common/R/lib.

To use it, just add the shared R library to your R library paths, as follows.
In general, the R_LIBS_SITE can be any colon-separated list of library paths.

export R_LIBS_SITE=/opt/apps/common/R/lib/%V/devtools

To verify the shared library is in your path:

module load r
R
.libPaths()
library("devtools")

Running R on Kamiak

An R job can be submitted the same as any other job.  Below is an example job submission script and an example R script.

#!/bin/bash
#SBATCH --partition=kamiak   # Partition (like a queue in PBS)
#SBATCH --job-name=R_example # Job Name
#SBATCH --output=R_%j.out
#SBATCH --error=R_%j.err
#SBATCH --time=0-01:00:00    # Wall clock time limit in Days-HH:MM:SS
#SBATCH --nodes=1            # Node count required for the job
#SBATCH --ntasks-per-node=1  # Number of tasks to be launched per Node
#SBATCH --cpus-per-task=1    # Number of threads per task (OMP threads)

cd "$HOME"
echo
echo "--- We are now in $PWD, running an R script ..."
echo

# Load R on compute node
module load r   # Use r/X.X.X for a specific version

Rscript --vanilla R_example.R
[~]$ cat R_example.R
HelloWorld <- function() {
    print('Hello World')
}

HelloWorld()

Submit the job and view its output:

[~]$ sbatch job_scripts/test_r_job.sh 
Submitted batch job 585419
[~]$ cat R_example.out

--- We are now in /home/my.NID, running an R script ...

[1] "Hello World"