Sample R lab (Hello world)

In this lab we will create a basic R script to print "Hello World!". Then we will use the scheduler to submit the job via sbatch.

The first step of this process is to either move your R script / data to the cluster. Or, you can simply start by creating a file, and adding R code to the file.

Go ahead and create a new file with your favorite editor and name it sample.R

When you have created the R file, add the following lines:

# A simple R script to print hello world!
aString = "Hello World!"

print (aString)

We can test that the script works by issuing Rscript to execute to script:

$ Rscript sample.R
[1] "Hello World!"

Great, now we know that our R script at least works. Next, lets submit this as a job to the scheduler. In order to do so, we need to create a script which will tell the scheduler what we want it to do. Below is an example script that you may use. For the purpose of this lab I have labeled the example script

#!/bin/bash -l
# How long should I job run for
#SBATCH --time=01:00:00
# Number of CPU cores, in this case 1 core
#SBATCH --ntasks=1
# Number of compute nodes to use
#SBATCH --nodes=1
# Name of the output files to be created. If not specified the outputs will be joined
#SBATCH --output=%x.%j.out
#SBATCH --error=%x.%j.err
# The code you want to run in your job
Rscript sample.R

Once your job script (the script you will se to submit to the cluster) and your code (the sample.R script) has been created. You can submit the job to the cluster with the sbatch command:

$ sbatch
Submitted batch job 3957

The returned value shows a successful job submission followed by a job ID. Once the job completes, I should now have two new files created in the directory.

$ ls | grep 3957

To verify the job executed my R code, I should see the print statement of "Hello World!" inside the output file

$ cat
[1] "Hello World!"



Article ID: 132372
Wed 5/12/21 1:52 PM
Wed 6/23/21 6:44 PM