#

R Basics

Basics

Step 0: To use R on the Odyssey 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 Odyssey training and be familiar with running jobs on Odyssey.

Installing local R packages

You may need to load packages or libraries to complete your work.

If you expect these libraries to be useful to others in the community, please send us a request to install them globally.

If you'd like to test, you can can install local copies in your home directory like so:

  • ensure the ~/.bashrc file has the following contents:
    export R_LIBS_USER=~/apps/R:$R_LIBS_USER
  • enter the command export R_LIBS_USER=~/apps/R:$R_LIBS_USER.
  • create these directories to hold your local packages: mkdir -pv ~/apps/R
  • for new modules, enter source new-modules.sh; module load R.
  • at the R prompt, you should be able to install packages now: install.packages("name of package here")
  • you'll be prompted to select a nearby CRAN mirror
  • R should then install the package

Once you've done all that, you can verify if your library is installed and should be able to work with it.

Running R Batch Jobs on Odyssey

To submit R jobs to Odyssey 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).

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

CC BY-NC 4.0 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Permissions beyond the scope of this license may be available at Attribution.