R

R is available on all HPC resources. To see what R versions are available via modules, do the following.

$ ml avail                         # check for "gnu" packages available
or
$ module spider R                  # check for how to check the module
$ module swap intel gnu7 or gnu8   # based on requirement
$ module avail R                   # lists all the avilable versions of "R"
$ module load  R                   # There are 3 versions of "R". This command loads latest version.
$ module load  R/3.4.4             # loads "R" version 3.4.4
$ module list                      # shows loaded module in your profile
If you require other versions of the software, please reach out to circ-assist@utdallas.edu.

You can now run the R CLI by typing the letter R or running R script. Don’t run your jobs on the login node. Instead, please submit your job to Slurm.

You need to create a Slurm job script to submit a job. Write your own script to submit a job (sbatch <script name>) as shown.

#!/bin/bash
#SBATCH --job-name=My-R-Job
#SBATCH --mail-user=netid@utdallas.edu  # netid example: xyz180012
#SBATCH --mail-type=ALL
#SBATCH --nodes=1
#SBATCH --ntasks=4
#SBATCH --output=r.result
#SBATCH --partition=normal

module swap intel gnu7/7.3.0             # for ganymede
module load R

Rscript  <script-name>
$ squeue -u $USER      # shows status of your job. Shows the nodes your job was submitted

          JOBID    PARTITION     NAME       USER      ST       TIME  NODES   NODELIST(REASON)
          219651  normal         My-R-Job  xyz180012   R       0:04      1   compute-7-3-12

Install packages

Installing R packages in Conda Environment

To install R essential packages in Conda environment do the following:

$ conda create -n my_R_env -c r r-essentials

-n  or --name      # name
my_env             # name of the virtual environment,
-c  or --channel   # channel
r                  # specified channel to install the `r-essentials` package from.

Conda will take some time to install the packages to the home directory. You get the prompt Proceed ([y]/n)?, hit y and then the Enter key to continue with the installation. The output after installation is:

r-dichromat-2.0_0    | 163 KB    | #################################################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate my_R_env
#
# To deactivate an active environment, use
#
#     $ conda deactivate

Use conda activate my_R_env to activate the newly created environment.

Use the conda list command to show the list of packages installed.

Use the grep command to quickly search and verify if a given package is installed.

$conda activate my_R_env                              # activate the newly created environment
(my_R_env)$ conda list                                # displays the list of packages installed
...
webencodings              0.5.1                    pypi_0    pypi
wheel                     0.37.1             pyhd3eb1b0_0
xz                        5.2.5                h7b6447c_0
zeromq                    4.3.4                h2531618_0
zlib                      1.2.12               h7f8727e_1
zstd                      1.4.9                haebb681_0

(my_R_env)$ conda list | grep -i wheel               # displays the package wheel
wheel                     0.37.1             pyhd3eb1b0_0

You can search for packages using multiple channels. For instance, to search for r-devtools in the r or conda-forge channels, use the following syntax.

(my_R_env)$  conda search -c r -c conda-forge r-devtools
Loading channels: done
# Name                       Version           Build  Channel
r-devtools                     1.8.0        r3.2.0_0  r
r-devtools                     1.8.0        r3.2.0_0  pkgs/r
r-devtools                     1.8.0       r3.2.0_0a  r
...
r-devtools                    1.12.0        r3.3.2_0  conda-forge
r-devtools                    1.12.0        r3.3.2_0  pkgs/r
r-devtools                    1.13.2        r3.3.2_0  conda-forge
r-devtools                    1.13.2        r3.4.1_0  r
...
r-devtools                     2.4.2   r40hc72bb7e_0  conda-forge
r-devtools                     2.4.2   r41hc72bb7e_0  conda-forge

Installing local packages in R

R packages can be installed from various resources using the following syntax

install.packages()             # Install a package from CRAN

biocLite()                     # Install a package from Bioconductor

devtools::install_github()     # Install a package from GitHub

installed.packages()           # View the list of installed packages

Example: to install the R package tester do install.packages("tester").

Useful R commands

.libPaths()                       # Folder containing installed packages

library()                         # Load and use an R package

search()                          # View loaded R packages

detach(pkg_name, unload = TRUE)   # Unload an R package

remove.packages()                 # Remove installed packages

update.packages()                 # Update installed packages

Parallelize R code with package makeCluster

The function to create a parallel socket cluster is makeCluster which takes in as an argument the number of cores.

makeCluster(numberOfCores)                 # Makes a cluster with specified number of cores.

makeCluster(spec, names, nnodes, type)     # Creates a set of copies of R running
                                             in parallel and communicating over sockets.

spec   # type of cluster

names  # number of copies to be run on localhost

nnodes # number of nodes to be forked

type # One of the supported type Ex: "MPI" or "PSOCK" or "FORK"

Example:

library(parallel)
numCores <- detectCores() - 1         # Calculate the number of cores, A good number of
                                        clusters is the numbers of available cores – 1.
numCores
## [1] 39
cl <- makeCluster(numCores)           # Initiate cluster, "cl". "cl", an object of class "cluster"
                                        make a cluster with 39 nodes
cl <- makeCluster(4)                  # Make a cluster with 4 nodes
str(cl)                               # Investigate the structure of "cl"
stopCluster(cl)                       # Close the cluster, "cl"