Submitting an R job is very similar to submitting a serial job shown on Submitting Jobs
Running R scripts in batch
There are two primary commands to use when submitting R scripts: `Rscript` and `R CMD BATCH`. Both commands will execute the passed script but differ in the way they process output.
Running R scripts using `R CMD BATCH`
When utilizing `R CMD BATCH` all output will be directed to an `.Rout` file named after your script unless otherwise specified. For example:
In the above example, output for the job will be found in the file `Rcode.Rout`. Notice that we did not specify output and error files in our SLURM directives, these are not needed as all R output will go into the `.Rout` file. To direct output to a specific location, follow your `R CMD BATCH` command with the name of the file where you want output directed to, as follows:
In this example, output from running the script `Rcode.R` will be placed in the file `Rcodeoutput.txt`.
To pass arguments to the script, they need to be specified after `R CMD BATCH` but before the script to be executed, and preferably preceded with `--args` as follows:
Running R scripts using `Rscript`
Using `Rscript` to execute R scripts differs from R CMD BATCH in that all output and errors from the script are directed to STDOUT and STDERR in a manner similar to other programs. This gives the user larger control over where to direct the output. For example, to run our script using `Rscript` the submit script could look like the following:
In the above example, STDOUT will be directed to the output file `TestJob.%J.stdout` and STDERR directed to `TestJob.%J.stderr`. You will notice that the example is very similar to to the serial example. The important line is the
module load command. That tells Tusker to load the R framework into the environment so jobs may use it.
To pass arguments to the script when using `Rscript`, the arguments will follow the script name as in the example below:
Multicore (parallel) R submission
Submitting a multicore R job to SLURM is very similar to Submitting an OpenMP Job, since both are running multicore jobs on a single node. Below is an example:
The above example will submit a single job which can use up to 16 cores.
Be sure to use limits in your R code so you only use 16 cores, or your performance will suffer. For example, when using the parallel package function mclapply:
Multinode R submission with Rmpi
Submitting a multinode MPI R job to SLURM is very similar to Submitting an MPI Job, since both are running multicore jobs on a multiple nodes. Below is an example of running Rmpi on Crane on 2 nodes and 32 cores:
When you run Rmpi job on Crane, please use the line export OMPI_MCA_mtl=^psm in your submit script. On the other hand, if you run Rmpi job on Tusker, you do not need to add this line. This is because of the different Infiniband cards Tusker and Crane use. Regardless of how may cores your job uses, the Rmpi package should always be run with mpirun -n 1 because it spawns additional processes dynamically.
Please find below an example of Rmpi R script provided by The University of Chicago Research Computing Center:
There are two options to install packages. The first is to run R on the login node and run R interactively to install packages. The second is to use the `R CMD INSTALL` command.
All R packages must be installed from the login node. R libraries are stored in user's home directories which are not writable from the worker nodes.
Installing packages interactively
- Load the R module with the command `module load R`
- Note that each version of R uses its own user libraries. To install packages under a specific version of R, specify which version by using the module load command followed by the version number. For example, to load R version 3.3, you would use the command `module load R/3.3`
- Run R interactively using the command `R`
- From within R, use the `install.packages()` command to install desired packages. For example, to install the package `ggplot2` use the command `install.packages("ggplot2")
Some R packages, require external compilers or additional libraries. If you see an error when installing your package you might need to load additional modules to make these compilers or libraries available. For more information about this, refer to the package documentation.
Installing packages using R CMD INSTALL
To install packages using `R CMD INSTALL` the zipped package must already be downloaded to the cluster. You can download package source using `wget`. Then the `R CMD INSTALL` command can be used when pointed to the full path of the source tar file. For example, to install ggplot2 the following commands are used:
Additional information on using the `R CMD INSTALL` command can be found in the R documentation which can be seen by typing `?INSTALL` within the R console.