What is R ?

R is a language and environment for statistical computing and graphics.  The R environment includes

  • a suite of operators for calculations on arrays, in particular matrices,
  • a large, coherent, integrated collection of intermediate tools for data analysis,
  • graphical facilities for data analysis and display either on-screen or on hardcopy, and
  • a well-developed, simple and effective programming language which includes conditionals, loops, user-defined recursive functions and input and output facilities.

R can be extended (easily) via packages. There are about eight packages supplied with the R distribution and many more are available through the CRAN family of Internet sites covering a very wide range of modern statistics.

R on ADA

Modules are avalable for 3.6.1 and 3.6.2

R has help built in, and a reference index is available.

Run R nteractively

Run conda interactively

[s154@login02 ~]$ interactive
srun: job 222161 queued and waiting for resources
srun: job 222161 has been allocated resources
[s154@c0002 ~]$ module add R/3.6.2

Run R in a batch job

  • An example of an R job :

#!/bin/bash
#SBATCH --mail-type=ALL                    #Mail events (NONE, BEGIN, END, FAIL, ALL)
#SBATCH --mail-user=<username>@uea.ac.uk           # Where to send mail
#SBATCH -p compute                            #Which queue to use
#SBATCH -t 36:00:00                               # Set time limit to 36 hours
#SBATCH --job-name=R-test_job        #Job name
#SBATCH -o R-test-%J.out                    #Standard output log  
#SBATCH -e R-test-%J.err                     #Standard error log
#set up environment

module add R/3.6.2
#run the application
R CMD BATCH TestRFile.R

R packages

R packages are a collection of R functions, complied code and sample data. They are stored under a directory called "library" in the R environment. By default, R installs a set of packages during installation. More packages are added later, when they are needed for some specific purpose. When we start the R console, only the default packages are available by default. Other packages which are already installed have to be loaded explicitly to be used by the R program that is going to use them.

All the packages available in R language are listed at R Packages.

Below is a list of commands to be used to check, verify and use the R packages.

Check Available R Packages

Get library locations containing R packages

.libPaths()

[1] "/gpfs/software/ada/R/3.6.2/lib64/R/library"

List all the packages installed

To see what packages are installed for the version of R you are using

library()

Packages in library ‘/gpfs/software/ada/R/3.6.2/lib64/R/library’:

base                    The R Base Package
boot                    Bootstrap Functions (Originally by Angelo Canty
                        for S)
class                   Functions for Classification
cluster                 "Finding Groups in Data": Cluster Analysis
                        Extended Rousseeuw et al.
codetools               Code Analysis Tools for R
compiler                The R Compiler Package
.....

List packages currently loaded in the R environment

search()
[1] ".GlobalEnv"        "package:stats"     "package:graphics"
[4] "package:grDevices" "package:utils"     "package:datasets"
[7] "package:methods"   "Autoloads"         "package:base"   

Load Package to Library

Before a package can be used in the code, it must be loaded to the current R environment. You also need to load a package that is already installed previously but not available in the current environment.

Load a package is loaded using

library("package Name", lib.loc = "path to library")

For example, load the package named "XML"

install.packages("./ XML_3.99-0.3.tar.gz ", repos = NULL, type = "source")

Install a New Package

If you need an additional package installed, you can fill in a software request, and we will install it for you into the global package library and it will be available to all users.

Alternatively, you can install R packages into your own personal library.  Because you don't have permission to write to the global package library, you will be prompted to use a personal library instead.  If a personal library doesn't already exist, you will be prompted to create a personal library:

There are two ways to add new R packages. One is installing directly from the CRAN directory and another is downloading the package to your local system and installing it manually.

Install directly from CRAN

Get the packages directly from CRAN webpage and installs the package in the R environment using install.packages("Package Name") . You may be prompted to choose a nearest mirror. Choose the one appropriate to your location.

For example, install the package named "XML".

install.packages("XML")

Install package manually

Go to the link R Packages to download the package needed. Save the package as a .gz file.

Now you can run the following command to install this package in the R environment

install.packages(file_name_with_path, repos = NULL, type = "source")

For example, install the package named "XML"

install.packages("./ XML_3.99-0.3.tar.gz ", repos = NULL, type = "source")