Search Docs by Keyword

Table of Contents

R – Basics

These instructions are not for RStudio using VDI. If you are using VDI, see RStudio Server VDI docs.


Basics

Step 0: To use R on the FASRC cluster, load the appropriate version available via our module system. See the modules list for available versions.

You should first have taken our Introduction to the FASRC training and be familiar with running jobs on the cluster.

Start an interactive job

An interactive job is the best way to provide a test environment while we are still working with our scripts.

salloc -p test --mem 1000 -t 30

Running R Batch Jobs on the cluster

To submit R jobs to the cluster via SLURM, the R command in your SLURM batch file should be in the format:

R CMD BATCH --quiet --no-restore --no-save scriptfile outputfile

where

--quiet

    • silences the startup messages so that they won’t appear in your output

--no-restore

    • does not restore the R workspace at startup

--no-save

    • does not save your R batch environment at exit

scriptfile

    • is your R script

outputfile

    is where all output will be sent

If you wish to pass along command line arguments in your SLURM batch script, you need to use the format:

R CMD BATCH --no-save --no-restore '--args a=1 b=c(2,5,6)' test.R test.out

and include the following lines in your R script:

##First read in the arguments listed at the command line
args=(commandArgs(TRUE))
##args is now a list of character vectors
## First check to see if arguments are passed.
## Then cycle through each element of the list and evaluate the expressions.
if(length(args)==0){
    print("No arguments supplied.")
    ##supply default values
    a = 1
    b = c(1,1,1)
}else{
    for(i in 1:length(args)){
      eval(parse(text=args[[i]]))
    }
}

print(a)
print(b)

Your output file test.out should have the following lines in it:

> print (a)
[1] 1
> print (b)
[1] 2 5 6

 

More examples and detail can be found at this helpful Stack Overflow webpage and the R doc pages.

You can also use the Rscript command. Please consult the the O’Reilly book R Cookbook for the difference between R CMD BATCH and RScript at O’Reilly Books Online for Harvard (valid Harvard ID required).

R Batch Script

To run R script as SBATCH script use the following template. Create R.batch file using the given template. You should make the requested changes to runtime, memory etc based on your needs.

#!/bin/bash
#SBATCH -c 1                # Number of cores
#SBATCH -t 0-00:10          # Runtime in D-HH:MM, minimum of 10 minutes
#SBATCH -p shared           # Partition to submit to
#SBATCH --mem=1000          # Memory pool for all cores (see also --mem-per-cpu)
#SBATCH -o myRjob_%j.out    # File to which STDOUT will be written, %j inserts jobid
#SBATCH -e myRjob_%j.err     # File to which STDERR will be written, %j inserts jobid

module load R #Load R module
R CMD BATCH --quiet --no-restore --no-save scriptfile outputfile

To submit the created script using sbatch R.batch

Running Large #s of R Batch Jobs

If you need to submit a large number of files (e.g. varying the parameters for jobs submitted), please see our documentation on Submitting Large Numbers of Files to the Cluster.

Installing Packages

See: R – Packages

© The President and Fellows of Harvard College
Except where otherwise noted, this content is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.