Installing R packages

This section provides information on how to install R package in your user environment.

The default R installation comes with a number of packages, but you will probably want to install R packages yourself at some point. It is possible to install R packages locally. There are different ways to do it and here will show you two options.

 

Keep in mind that users do not have sudo permissions on the clusters.

If you need assistance with installing software, please reach out to the support team for your cluster.

Pre-installed R packages

Before you try to install packages, load the module for R, start R and check if the package is already installed. For example like this after starting an R session on the command line:

> library(doParallel) Loading required package: foreach Loading required package: iterators Loading required package: parallel

If the package is not installed, it would show you an error message:

> library(test) Error in library(test) : there is no package called ‘test’

Getting a list of installed packages

You can get a list of installed R packages with the following command after starting an R session:

installed.packages()

Note that the output can be quite long it might take a bit for R to get a list of all the installed packages.

With the following commands, you can get additional information

where you replace <package> with the name of the package.

R package repositories

ou can find a list of R packages here:

The package repository for R is mirrored on various server in many countries in order to allow users to select a mirror geographically close to them.

Always use a mirror server that you trust

You can get a list of available mirror servers with the R command

And for a given session, you can set the mirror server with

Installing R packages locally

Before you submit your R job (for the first time), it is best to load R directly on the login node, set up the directory for the packages that you want to install locally and install them.

By default, local R packages are installed in:

where <version> will be replaced by the version of R that you use. This means that if you would switch to a different version of R, you would have to install all packages again.

Before we can install packages, login in to the cluster, load an R package of your choice and start an R session, e.g.


ALICE


SHARK


When you try to install a package the first time, you will see the following message:

Answer yes and R will ask you whether it can create a directory for you

Answer yes again and R will create the directory $HOME/R/x86_64-pc-linux-gnu-library/3.6 to install packages. Next, R will prompt you with a list of repositories to install the package from if you have not already set one for this session.

This list of mirror servers is subject to change which is why we do not highlight a specific number.

Type in the number of the repository that you would like to choose. There are many options and we recommend to choose an option that is geographically close. After you have selected a repository, R will proceed to install the package.

Unfortunately, R does not remember the repository that you have chosen, so for each package that you want to install, you will have to specify the repository. However, there are solutions for that:

Setting the repository

The simplest way is setting the repository in the installation command, e.g.,

Here, we used the repository from the Friedrich-Alexander University Erlangen-Nuremberg in Germany. Just as a reminder, make sure that you use only trusted repository.

An alternative method is to create the file .Rprofile in your home directory with the following content:

Then, you do not have to use the "repos" parameter in the install.packages() command anymore.

By using one of the two methods for setting the package repository, it is possible to install packages as part of your job if you really need to do it. However, since the install.packages() command does not check if a package is already installed, but instead always installs the given package, it makes sense to build your R environment first manually or in a separate installation job.

Setting the local installation directory

You can also specify a custom directory for installing R packages. First, create the directory where the packages should be installed, e.g.,

Next, you create the file .Renviron in your home directory and save in it R_LIBS_USER with the location of the dirctory, e.g.,

Alternatively, you can set the location as a command line variable before starting R using R_LIBS instead, e.g.,

If you do this before you install packages locally the first time, R will not ask you for creating a local directory.

Setting the download directory

By default, R will download packages to /tmp. This is not always useful if space on /tmp is limited You can also specify a download directory of your own in the install.packages command, e.g.,

Managing installed R packages

Removing packages

For removing packages, load the module for R and start R directly from the command line. Then use the remove.packages

You cannot remove packages that are installed globally.

Updating R packages

You can check what packages need an update with a call to the function:

Updating packages (local):

Or:

for without asking

If a global R package needs to be updated, contact the support team for your cluster.

Â