Skip to content
Bart Verleye edited this page Mar 10, 2015 · 74 revisions

Table of Contents

Details

  • Homepage: http://www.r-project.org/
  • Versions available:
    • 3.0.1-goolf-1.4.10-bare
    • 3.0.3-goolf-1.5.14
    • 3.1.1-goolf-1.5.14
    • 3.1.1-iomkl-6.5.4
    • 3.1.2-goolf-1.5.14
  • Licensing: free

R is a language and environment for statistical computing and graphics. It is a GNU project which is similar to the S language and environment which was developed at Bell Laboratories (formerly AT&T, now Lucent Technologies) by John Chambers and colleagues. R can be considered as a different implementation of S. There are some important differences, but much code written for S runs unaltered under R. R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, ...) and graphical techniques, and is highly extensible. The S language is often the vehicle of choice for research in statistical methodology, and R provides an Open Source route to participation in that activity.

Usage

Plots and graphics

Plots usually require interactive operations/windows to be commenced. To avoid this and to capture graphical output redirection can be used. Below is an example of redirecting plots to an external png file:

png(filename="plot.png")  # This line is added to redirect plots below to plot.png file
par(mfrow=c(2,1))
plot(dispersed,type="l",ylab="dispersed")
plot(nondispersed,type="l",ylab="nondispersed")

Installing libraries

If you can see the list of available libraries in by typing library() at the R prompt.

R
...
> library()

To install more libraries use install.packages("package_name"). It's best to do this from one of the build nodes, for example, to install the sampling library:

ssh build-gpu-p
R
...
> install.packages("sampling")

You may be asked to select a directory for installing libraries. The default location is recommended. If you are installing several packages, you may be asked to choose a download mirror. New Zealand should be listed.

You can confirm the library has been installed by using the library() command:

> library(package_name)

At the R command prompt when you want to quit R type quit(). You will be asked "Save workspace image? [y/n/c]". Type n. If you have any issues or questions about installing R libraries, please contact us.

Compiling Libraries

You can compile custom C libraries for use with R using the R shared library compiler. It is best to do this from one of the build nodes:

ssh build-gpu-p
R CMD SHLIB mylib.c

This will create the shared object mylib.so. You can then reference the library in your R script

dyn.load("~/R/lib64/mylib.so")

Example SLURM jobs

Example_3_MPI

Job description (download)
#!/bin/bash
#SBATCH -A uoa12345
#SBATCH --ntasks=12
#SBATCH --cpus-per-task=1
#SBATCH --time=01:00:00
#SBATCH --mem-per-cpu=2G
module load R

# Give this script doMPI_example.R or snow_example.R as an argument (ie: $1)
# Our R has a patched copy of the snow library so that there is no need to use RMPISNOW.
srun Rscript $1

Input file(s)

Example_1_Simple

Job description (download)
#!/bin/bash
#SBATCH -J R-test
#SBATCH -A uoa12345         # Project Account
#SBATCH --time=01:00:00     # Walltime (hh:mm:ss)
#SBATCH --mem-per-cpu=2048  # memory/cpu (in MB)
module load R
srun Rscript test.R

Input file(s)

Example_2_Array

Job description (download)
#!/bin/bash
#SBATCH -J R-array
#SBATCH -A uoa12345         # Project Account
#SBATCH --time=01:00:00     # Walltime (hh:mm:ss)
#SBATCH --mem-per-cpu=2048  # memory/cpu (in MB)
#SBATCH --array=1-10        # Array definition (max 1000 jobs)

# SLURM creates an environment variable that holds the array id (e.g 1 - 10)
# This is useful if you want to read a filename unique to the job e.g input_1.txt, input_2.txt, ... input_10.txt
# It is also useful as a random seed that guarntees each job will draw random numbers in a different sequence.

# You can access this value from within R using Sys.getenv e.g:
# jobid = as.numeric(Sys.getenv("SLURM_ARRAY_TASK_ID"))

# Load the default R environment, for specific versions available see: module spider R
module load R
srun RScript array.R

Input file(s)
Clone this wiki locally