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.


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



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


    • does not restore the R workspace at startup


    • does not save your R batch environment at exit


    • is your R script


    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 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.
    print("No arguments supplied.")
    ##supply default values
    a = 1
    b = c(1,1,1)
    for(i in 1:length(args)){


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.

#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.