- Introduction
- Versions
- Adding R to Your Environment
- Installing Add-on Packages (in user specified locations)
- Viewing Installed R Packages
Introduction
R is a programming language and software environment for statistical computing and graphics. R and its libraries implement a wide variety of statistical and graphical techniques, including linear and non-linear modelling, classical statistical tests, time-series analysis, classification, clustering, and others. R is easily extensible through functions and extensions, and the R community is noted for its active contributions in terms of packages. R is also an interpreted language in which users typically access it through a command-line interpreter.
R and R packages are available via the “Comprehensive R Archive Network” (CRAN), a collection of sites which carry identical material, consisting of the R distribution(s), the contributed extensions, documentation for R, and binaries.
Versions
The table below list the versions of R installed on the Campus Cluster.
Version |
---|
R 3.5.1 |
R 3.6.2 |
R 4.0.1 |
R 4.0.2 |
R 4.1.1 |
Adding R to Your Environment
Each R installation on the Campus Cluster has a module that you can use to load a specific version of R into your user environment. You can see the available versions of R by typing module avail R
on the command line. The latest version of R available on the Campus Cluster can be loaded into your environment by typing module load R
To load a specific version you will need to load the corresponding module. See the section Managing Your Environment in the User Guide for more information about modules.
Installing Add-on Packages (in user specified locations)
Any R add-on package not available in the system installation (see here for information on viewing the list) can be installed from the CRAN in a user specified location that you have write access to with just a few easy steps.
Generally when installing any software there are a few things to consider
- Home directory disk usage quotas.
- Will the software be used by multiple users in my group?
(Project space is the **recommended** location to install software.) - Does the desired software have dependencies/requirements?
- Is the prerequisite software already installed?
Installation Command Syntax
R package installations for the most part are straight forward. Minimally, all that is needed is
the name of the package that is to be installed, but installation location and the repository can
also be specified.
Command (“Package Name”, “Location”, “Repository”) | Behavior |
---|---|
install.packages('package_name', '/path/to/r_libraries', 'Repository URL') |
Installs the package downloaded from the specified repository in the specified location |
Command (“Local Filename”, “Location”, “Repository”) | Behavior |
install.packages('package_name.tar.gz', '/path/to/r_libraries', repos = NULL) |
Installs the local package(file) in the specified location specifying no repository(NULL) |
When the installation location and the repository URL are not specified, R packages are installed
in a default location and the R installation process prompts to choose from a list of repositories.
R packages downloaded manually from the CRAN can be
installed by specifying the local filename and omitting the repository URL (specifying NULL).
Using Rscript
To run R commands without starting an R session the command Rscript
can be used (as we show in the examples on this page). As a scripting front end for R, Rscript
enables the use of R via shell scripts and scripting applications.
Installation Steps
Below are steps with examples of the actual commands that can be run on the Campus Cluster. In these steps ~/Rlibs is used for the location to install user specific add-on packages (The tilde “~” means the users’ home directory—i.e. $HOME).
Note: It is assumed that these steps are being run under the default BASH shell. When using a different shell the syntax for that shell should be used where appropriate.
- Create a directory for your R packages
mkdir ~/Rlibs
- Load the R modulefile
module load R/4.1.1
- Set the R library environment variable (R_LIBS) to include your R package directory
export R_LIBS=~/Rlibs:${R_LIBS}
- Use the
install.packages
function to install your R package.
Rscript -e "install.packages('RCurl', '~/Rlibs', 'http://ftp.ussg.iu.edu/CRAN')"
If the environment variable R_LIBS is not set and a directory is not specified with the install.packages command, then R packages will be installed under ~/R/x86_64-unknown-linux-gnu-library by default. (This R subdirectory structure is created automatically.)
The R_LIBS environment variable will need to be set every time when logging in to the Campus Cluster if the user’s R package location is to be visible to an R session. The following:
if [ -n $R_LIBS ]; then export R_LIBS=~/Rlibs:$R_LIBS else export R_LIBS=~/Rlibs fi
can be added to a user’s ~/.bashrc file to remove the need to set the R_LIBS environment variable with every login session to the Campus Cluster.
Warnings and Error Messages
R packages that are not available in the current CRAN (Comprehensive R Archive Network) or if the name of the package is misspelled, tend to generate a message similar to the following:
[golubh1 ~]$ Rscript -e "install.packages('phybase','~/Rlibs', 'http://ftp.ussg.iu.edu/CRAN')"
Warning message:
package 'phybase' is not available (for R version 3.2.2)
Searching the CRAN site for your desired R package may provide links to archived versions that are not available in the current CRAN. In this case the specific archived R package can be downloaded and installed from the local file using the same command but omitting the repository URL (specifying NULL).
Some R packages have dependencies and require them to be installed first and will generate an error message similar to the following:
[golubh1 ~]$ Rscript -e "install.packages('phybase_1.1.tar.gz', '~/Rlibs', repos = NULL)"
ERROR: dependency 'ape' is not available for package 'phybase'
* removing '/home/jdoe/Rlibs/phybase'
Warning message:
In install.packages("phybase_1.1.tar.gz", repos = NULL) :
installation of package 'phybase_1.1.tar.gz' had non-zero exit status
Installing the required R package first then the desired R package resolves this issue.
Viewing Installed R Packages
The library() command can be used to view all user and system installed R packages (user installed packages are only visible to R when the $R_LIBS environment variable is set).
[golubh1 ~]$ Rscript -e "library()" Packages in library '/home/jdoe/Rlibs': R6 Classes with reference semantics RCurl General network (HTTP/FTP/...) client interface for R ... stringr Simple, Consistent Wrappers for Common String Operations whisker {{mustache}} for R, logicless templating Packages in library '/usr/local/R/R-3.5.1/lib64/R/library': KernSmooth Functions for kernel smoothing for Wand & Jones (1995) MASS Support Functions and Datasets for Venables and Ripley's MASS ... tools Tools for Package Development utils The R Utils Package