Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This section

is still under development

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.

Note

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.

Table of Contents
minLevel1
maxLevel2

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:

Code Block
> 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:

Code Block
> 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:

Code Block
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

Code Block
packageDescription("<package>")
help(package = "<package>")
packageVersion("<package>")

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.

Note

Always use a mirror server that you trust

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

Code Block
getCRANmirrors()

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

Code Block
chooseCRANmirror()

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:

Code Block
$HOME/R/x86_64-pc-linux-gnu-library/<version>

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

Code Block
  [me@nodelogin02 ~]$ module load R/4.0.5-foss-2020b
  [me@nodelogin02 ~]$ R

SHARK

Code Block
  [me@res-hpc-lo02 ~]$ statistical/R/4.1.2/gcc.8.3.1
  [me@res-hpc-lo02 ~]$ R

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

Code Block
Warning in install.packages("test") :
  'lib = "<global_path_to_R>/lib64/R/library"' is not writable
Would you like to use a personal library instead? (yes/No/cancel)

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

Code Block
Would you like to create a personal library
   ‘~/R/x86_64-pc-linux-gnu-library/3.6’
to install packages into? (yes/No/cancel)

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.

Code Block
--- Please select a CRAN mirror for use in this session ---
Secure CRAN mirrors
 
1: 0-Cloud [https]
...

Selection:
Info

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.,

Code Block
 install.packages("foreach", repos="https://ftp.fau.de/cran")

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:

Code Block
# setting the default R repository
repo = getOption("repos")
repo["CRAN"] = "https://ftp.fau.de/cran"
options(repos = repo)
rm(repo)

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.,

Code Block
 [me@nodelogin02 ~]$ mkdir ~/datadata1/R_pkgs

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.,

Code Block
 R_LIBS_USER=~/datadata1/R_pkgs/

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

Code Block
 [me@nodelogin02 ~]$ export R_LIBS="~/datadata1/R_pkgs/"

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.,

Code Block
 > install.packages(..., destdir="<directory_for_downloads>")

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

Code Block
 > remove.packages(<name_of_package>)

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:

Code Block
old.packages()

Updating packages (local):

Code Block
update.packages()

Or:

Code Block
update.packages(ask = FALSE)

for without asking

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