Skip to main content Skip to navigation
Kamiak Programmer's Guide

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.

Installing R Packages/Libraries

The installation of R on Kamiak includes base packages, but not many others.  Additional packages are 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.  Below is an example of installing an R package to a home directory:

  1. Load your chosen version of R:
    [~]$ module load r/3.3.0
  2. Create a directory to store installed packages:
    [~]$ mkdir -pv ~/lib/R_libs
  3. Modify your “.bash_profile” file (note the leading dot or “.”) to tell R where to install packages, then make that change effective to the current login shell:
    [~]$ echo export "R_LIBS_USER=\${HOME}/lib/R_libs" >> .bash_profile
    [~]$ source .bash_profile
  4. 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'))

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 -o R_example.out     ### File in which to store job output
#SBATCH --time=0-00:10: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)
#SBATCH --get-user-env       ### Import your user environment setup
#SBATCH --verbose            ### Increase informational messages

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

# Load R on compute node
module load r/3.3.0

Rscript --vanilla 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"