R is an interpreted computer language for statistical computing and graphics including: linear and generalised linear models, non-linear regression models, time series analysis, classical parametric and non-parametric tests.
For efficiency, procedures written in the C, C++, or Fortran languages may be interfaced within R in addition to the user-visible R functions.
R is available as a module on Apocrita.
To run the default installed version of R, simply load the
$ module load R $ R --help Usage: R [options] [< infile] [> outfile] or: R CMD command [arguments] $ Rscript --help Usage: /path/to/Rscript [--options] [-e expr [-e expr2 ...] | file] [args]
For further usage documentation, see the full output of
R --help or the
man R and
R can be executed through the 'R CMD' interactive interface using
R or via
Rscript non-interactive interface. The
Rscript binary provides an
alternative front end to the legacy
R CMD BATCH method to run R commands
in a non-interactive shell.
Do not use
detectCores() is not cluster aware and will incorrectly return all cores
on a machine even if they are not actually available.
The documentation for R covers this:
"This is not suitable for use directly for ... specifying the number of cores in
First because it may return NA, second because it does not give the number of allowed cores"
We advise specifying the number of cores directly, using the
NSLOTS UGE variable:
library(parallel); cluster <- makeCluster(as.integer(Sys.getenv('NSLOTS')))
Here is an example job running on 2 cores and 4GB total memory:
#!/bin/bash #$ -cwd #$ -j y #$ -pe smp 2 #$ -l h_rt=1:0:0 #$ -l h_vmem=2G module load R Rscript myscript.R
Rscript within Apocrita jobs
When running R jobs on Apocrita, use
Rscript to submit a file containing
R commands; R output will be written to the
Additional R Packages¶
Additional packages can be installed to extend the capabilities of R; packages are stored in libraries.
To display the list of active libraries, execute:
> .libPaths()  "/share/apps/centos7/R/<VERSION>/lib64/R/library"
To display the list of currently loaded packages (including default packages), execute:
> (.packages())  "stats" "graphics" "grDevices" "utils" "datasets" "methods"  "base"
To display all packages available in the currently loaded libraries, execute:
> .packages(all.available = TRUE)  "base" "boot" "class" "cluster" "codetools"  "compiler" "datasets" "foreign" "graphics" "grDevices" ...
To install additional R packages, execute:
Packages which are not included in the R main library will be added to the personal library.
Creating a personal library
If a personal library has not already been created, respond with
prompted to create and use a personal library. Also select your closest
Secure CRAN mirror.
To load an R package, execute:
To find the location where a package is installed, execute:
> find.package("<package_name>")  "/path/to/package"
R packages with external dependencies¶
Installing an R package will automatically install other R packages on which that package depends. However, R packages often depend on software packages provided outside R, such as C libraries. These packages may be provided by a module on Apocrita instead of being available by default. To access these packages be sure to load the Apocrita module before starting your R session.
For example, the module
gdal is required to build the
rgdal R package and
gmp is required for several numerical R packages.
R packages using code in other languages¶
If a package requires a non-default compiler to build then you can load a
R packages which compile code written in other languages, such as C or Fortran,
often use a
Makevars options file to determine how that code is compiled. If
you wish to tailor the building of packages to suit your needs you can create
Using a personal
Makevars file when building packages
~/.R/Makevars will be used by default if it exists. You can use
a different file in a specific session by setting the environment variable
R_MAKEVARS_USER to point to the file.
Error: C++14 standard requested but CXX14 is not defined
Some R packages rely on a C++ compiler which supports the C++ 14 language
revision. If package building fails with an error about "
CXX14" then you
can add the following line to the
Makevars file in use, to compile with
CXX14=g++ -std=c++14 -fPIC
You will also need to load a compiler module, such as
use a compiler which supports this language revision. You should load
this, and any other module, before starting your R session.
Bioinformatics packages for R¶
Bioinformatics packages for R can be installed with the bioconductor script and then loaded like a normal R package. For versions of R starting with 3.6.1 you can install a package using:
# Installing bioinformatics packages for R >=3.6.1 install.packages("BiocManager") BiocManager::install("<package_name>")
For some earlier releases of R this method does not work and you should instead use:
# Installing bioinformatics packages for R <3.6.1 source("https://bioconductor.org/biocLite.R") biocLite("<package_name>")