Using R in batch
Batch mode¶
There are two senses of 'batch' at play: One is R's sense of batch, and the other is the sense of 'batch job'. On the cluster, you may need to run R in a batch job, so you need some way to tell R to run the commands in a file.
One way is to use R's batch mode, with which R's output will go to a file
rather than the screen. The other way is to use Rscript
, which prints
output to standard out, and in the context of a batch job, that will be the
job's output file.
Using R's batch mode¶
For longer jobs, or if you need to run many similar R programs, you will want
to run those in a batch job. There are two ways to run an R script as if it
were a program, one uses the R
command and the the other uses Rscript
.
For the following examples, you should make a test directory and copy our
example R script to it using these commands (the $
is the prompt; do not
type it).
$ mkdir Rtest
$ cd Rtest
$ cp /gpfs1/sw/examples/R/iris.R .
$ module load R
Using R CMD BATCH
¶
The first method of running an R script in batch is to use the usual R
but to add the CMD
parameter. In its simplest form that looks like this:
$ R CMD BATCH iris.R
That will instruct R to run the commands in iris.R
and put the output into
a file called iris.Rout
in the same directory. If you want to rename the
output file, add the desired output file name to the command, as in
$R CMD BATCH iris.R iris.txt
Using Rscript
¶
Rscript
sends output to standard output, so, when you run it from an
interactive prompt, the output appears on screen.
$ Rscript iris.R
Sepal.Length Sepal.Width Petal.Length Petal.Width
Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
Median :5.800 Median :3.000 Median :4.350 Median :1.300
Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
Species
setosa :50
versicolor:50
virginica :50
If you use Rscript
in a batch job, output will go to the Slurm output file
instead of the screen.
Using R in a batch job¶
To use R from a (Slurm) job, you need to create a job script. Here is an
example for you to put into a file called r-cmd.sbat
in the Rtest
folder.
#!/bin/bash
#### Slurm preamble to define the job
#SBATCH --job-name=R_example
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=1
#SBATCH --cpus-per-task=1
#SBATCH --mem=4gb
#SBATCH --time=5:00
#### End preamble
#### Commands to run in the job
## Clear all modules and load only those needed for the job
module purge
module load R
## Run your commands
echo "I ran from $(pwd) on $(date)"
R CMD BATCH iris.R
The echo
line prints some information to the standard output; otherwise
the Slurm output file would be empty unless error messages were printed.
R CMD BATCH
doesn't normally produce any output. To submit the job, use
$ sbatch r-cmd.sbat
Submitted batch job 12345
The output from this job will be in a file called slurm-12345.out
in the
directory from which you submitted the job, and it will contain just the
results of running the echo
command.
$ cat slurm-12345.out
I ran from /gpfs1/home/u/s/user/Rtest on Tue May 20 12:05:53 PM EDT 2025
and the R output will be in iris.Rout
.
Copy r-cmd.sbat
to rscript.sbat
and change the last line to read
Rscript iris.R
and then submit it with
$ sbatch rscript.sbat
Submitted batch job 12346
When it completes, both the output from the echo
command and from Rscript
will be in slurm-12346.out
.
$ cat slurm-12346.out
I ran from /gpfs1/home/u/s/user/Rtest on Tue May 20 12:15:55 PM EDT 2025
Sepal.Length Sepal.Width Petal.Length Petal.Width
Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
Median :5.800 Median :3.000 Median :4.350 Median :1.300
Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
Species
setosa :50
versicolor:50
virginica :50