R is a system for statistical computation and graphics. It consists of a language plus a run-time environment with graphics, a debugger, access to certain system functions, and the ability to run programs stored in script files. Some of R’s main features include:
R
.
module load python3/anaconda/5.2.0
conda create -n r-environment r-essentials r-base
source activate r-environment
export R_LIBS_USER=/home/$USER/.conda/envs/r-environment/lib/R/library/
Once the virtual environment is created, it can be launched at any time by ensuring that the python3 module is
loaded, using the command
module load python3/anaconda/5.2.0
source activate r-environment
install.packages("ggplot2")
source deactivate
2. Create a R script. This repository provides a simple script, test.r, which demonstrates some of R's basic features.
print("Starting tests.")
x <- 1:10 # initializes x as values from 1 to 10
print(x) # print x
y <- sample(1:100, 10, replace=T) # generate 10 random numbers from 1 to 100
print(y) # print y
mean(x) # find mean of x
print(x*y) # print product of x and y
a <- c(2, 4, 6, 8, 10, 12) # a is a vector
b <- a[a>4] # b is the vector of all indices in a greater than 4
print(a)
print(b)
3. Prepare the submission script, which is the script that is submitted to the Slurm scheduler
as a job in order to run the R script. This repository provides the script job.sh as an example.
#!/bin/sh
#SBATCH --job-name=r_test
#SBATCH -o r_out%j.out
#SBATCH -e r_err%j.err
#SBATCH -N 1
#SBATCH --tasks-per-node=1
echo -e '\n submitted R job'
echo 'hostname'
hostname
# loads python module
module load python3/anaconda/5.2.0
# activates R virtual environment
source activate r-environment
# runs R program
R < test.r --no-save
# exit the virtual environment
source deactivate
4. Submit the job using: sbatch job.sh
5. Examine the results.