R on MonARCH

What is R

R is GNU S - A language and environment for statistical computing and graphics. R is similar to the award-winning S system, which was developed at Bell Laboratories by John Chambers et al. It provides a wide variety of statistical and graphical techniques (linear and nonlinear modelling, statistical tests, time series analysis, classification, clustering, …).

R is designed as a true computer language with control-flow constructions for iteration and alternation, and it allows users to add additional functionality by defining new functions. For computationally intensive tasks, C, C++ and Fortran code can be linked and called at run time.

There is an Australian AARNet mirror of the main R web site. http://en.wikipedia.org/wiki/R_%28programming_language%29

Sample Code

Place this code in a file, i.e. ** example.r **

x <- c(1,2,3,4,5,6)   # Create ordered collection (vector)
y <- x^2              # Square the elements of x
print(y)              # print (vector) y
mean(y)               # Calculate average (arithmetic mean) of (vector) y; result is scalar
var(y)                # Calculate sample variance
lm_1 <- lm(y ~ x)     # Fit a linear regression model "y = f(x)" or "y = B0 + (B1 * x)"
                        # store the results as lm_1
print(lm_1)           # Print the model from the (linear model object) lm_1
summary(lm_1)         # Compute and print statistics for the fit
# of the (linear model object) lm_1
par(mfrow=c(2, 2))     # Request 2x2 plot layout
plot(lm_1)             # Diagnostic plot of regression model

Write a SLURM submission script

#!/bin/env bash
#SBATCH --job-name=myFirstR
#SBATCH --time=00:10:00
#SBATCH --mem=3000
#SBATCH --ntasks=1
#SBATCH --mail-type=BEGIN,END,FAIL
#SBATCH --mail-user=my.username@monash.edu
#SBATCH --output=output.txt
module load  R/4.2-mkl
R --vanilla < example.r
# or R --vanilla < example.r > output.txt

Submit via SLURM

sbatch submit.sh

Installing Modules in R

As Monarch is a central cluster used by many people and groups, we are concerned that by adding additional modules, existing modules may be changed (by updating) or by even breaking them. As such, users are requested to install additional modules themsleves by the procedures mentioned below. If you feel that a large number of people would be using the additional module, please let us know, and we will add it. All new module libraries added to our R installation will be implemented by a clean install of the R libraries, to ensure backward compatibility

To add a new library

  1. Setup the local library folder as follows:

mkdir -p  ~/R/libs; export R_LIBS=~/R/libs
  1. load the R module

module load R
#for default version, or use a version number i.e. module load R/4.2.2-mkl
  1. run the command within R as follows:

install.packages('dbscan', repos='https://cloud.r-project.org')
  1. Updated your profile (.bash_profile and .bashrc) to include this line:

export R_LIBS=~/R/libs

so that every time you use R/3.2.2, the ‘dbscan’ package should be available to use.