Overview
To run an R job as a batch submission, you can include all of your R commands in a single file and execute it with Rscript
.
Submission Script
To run an R script in batch, you’ll need to start by creating a batch script. In this example, we’ll call it submit_r_script.slurm
. Start by creating a batch script in your working directory using a command like:
[netid@wentletrap ~]$ touch submit_r_script.slurm
Then open it in your favorite text editor. You will then add the contents below. The first lines that start with #SBATCH
are how you request resources and set job options through the scheduler SLURM. The second part includes any commands you would need to run in the terminal to execute your workflow. This includes loading any modules, changing directories, executing scripts, etc. To run an R script in batch, you will use the command Rscript
and not R
. An alternative is R CMD BATCH
. More information can be found here.
#!/bin/bash
#SBATCH --job-name=R-Plotting-Job
#SBATCH --ntasks=1
#SBATCH --nodes=1
#SBATCH --time=00:01:00
#SBATCH --partition=standard
#SBATCH --account=YOUR_GROUP
module load R/4.0.0
Rscript hello_world.r
Once you’ve entered the contents into your text file, save and close.
R Script
Now, we’ll create a simple R script that will print the line Hello World!
. We will call this script hello_world.r
and can create it using the same method we used for the batch script.
[netid@wentletrap ~]$ touch hello_world.r
Now, open the file in your favorite text editor and enter the following:
#!/usr/bin/env Rscript
hello_string <- "Hello World! "
print (hello_string, quote=FALSE)
Now, save and exit.
Job Submission
To run the batch job, submit it to the scheduler using the command sbatch
followed by the SLURM file’s name. This will return a job ID that you can use to track the status of your job.
[netid@wentletrap ~]$ sbatch submit_r_script.slurm
Submitted batch job 53341
Output
An output file will be generated by the scheduler containing any output that would have been printed to the terminal had you run your batch script interactively.
[netid@wentletrap ~]$ cat slurm-53341.out
[1] Hello World!