Skip to content

Installing R packages

You can install R packages of your own without the intervention of the VACC system administrators. There are a couple of things to note before you start installing packages of your own.

  • Packages are installed into directories, which are then called 'libraries'.

  • R will, by default, use one library directory for all your R libraries, so if you wish to have something like a 'virtual environment' with the VACC installed R modules, you may need to manually specify library paths.

  • You only need to install packages once. Once the packages are installed, they are available for every subsequent invocation of R (see below about R versions and the R library hierarchy). Please check R scripts that might run the install.packages() program every time and remove those invocations.

  • If you will be installing R packages from RStudio, you must make sure that no project is active. If there is a project active, packages will not install properly.

R looks for packages (libraries) in three places specified by three variables: R_LIBS, R_LIBS_SITE, and R_LIBS_USER. Those can be set to make separate collections of libraries available when you need them. Those three locations will be explained next, and then we'll turn to examples of installing R packages.

R library hierarchy

The three variables above can be used to specify locations for R libraries. The first, R_LIBS, is the library folder in which packages installed with the base R can be found. Only system administrators can use this library path.

The second, R_LIBS_SITE, is also used by the system administrators. Users cannot write to that location either, which is used for both the VACC Tidyverse installation and for the geospatial packages included in what we make available as the Rgeospatial module.

The third, R_LIBS_USER, should be used by regular users to provide the location of user-installed packages. By default, this is set to ~/R/x86_64-pc-linux-gnu-library/<version>, where <version> is the first two version numbers of the installed R. For example, if R 4.4.2 is used, then that location would be ~/R/x86_64-pc-linux-gnu-library/4.4. If the version of R changes to 4.4.4, packages installed there will be available; however, if it changes to 4.5.0 or 5.0.0, then all R packages would need to be reinstalled.

Installing a package

Packages are installed using the R install.packages() function. At a bare minimum, you need to specify the package name, in quotes. In the following examples, > is intended to be the R prompt; do not type or copy it if you wish to run the command. For example,

> install.packages('assert')

If you do not have a personal library, R will tell you it can't write to the libraries it knows about and ask if you want to use a personal library; if you answer yes, it will ask if you want to create one, which you do. R will then ask whence it should get the package you've told it to install, and it will print a list indicating the country and location therein of the package repositories. You should pick one in the USA (we typically use either IA or MI from habit) by typing its number. When this tutorial was prepared, the USA list appeared as

67: USA (IA) [https]
68: USA (MI) [https]
69: USA (MO) [https]
70: USA (OH) [https]
71: USA (OR) [https]
72: USA (PA 1) [https]
73: USA (TN) [https]
74: USA (UT) [https]

and we chose 68, at which point R said it was trying, then printed the steps it completed to install the package. That looked like

Selection: 68
trying URL 'https://mirror.its.umich.edu/cran/src/contrib/assert_1.0.1.tar.gz'
Content type 'application/x-gzip' length 2207 bytes
==================================================
downloaded 2207 bytes

* installing *source* package ‘assert’ ...
** package ‘assert’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (assert)

The downloaded source packages are in
    ‘/tmp/RtmpLItaJB/downloaded_packages’

You can tell it succeeded because of the line that says DONE (assert). The downloaded source packages will be deleted when you exit R.

Installation options

The install.packages() function can take additional arguments/options, many of which are fairly advanced. We'll look at two here. The first is that you can skip the step where it asks whence you would like the packages fetched by specifying the mirror site. In our example above, we could have said USA (MI) by using this modified command.

> install.packages('assert', repos="https://mirror.its.umich.edu/cran")

You can find a list of mirror sites at CRAN and clicking on the Mirrors link in the menu. Use your browser search to find 'USA' to get the USA site addresses.

You could also tell it to install to an alternate library location by including the directory path in quotes after the package name. For example, if you wanted to use ~/Rtest as your library, you would first create that directory, then use

> install.packages('assertions', '~/Rtest', repos="https://mirror.its.umich.edu/cran")

Removing packages

You can remove a package using, for example remove.packages('assert'). If the package is installed in a library other than your personal library, as would be the case with the assertions package installed above, you must specify from which library to remove it, as in

> remove.packages('assertions', '~/Rtest')

Switching among libraries

If you need to have more than one library -- say you are updating an R program that used an older version of R and older packages, and you want to install the packages for the current version into a separate directory -- then you can do so by:

  • Creating a separate directory for the new installation

  • When you wish to use the new version, set the environment variable R_LIBS_USER to the name of the new directory.

When R starts, it will read the R_LIBS_USER variable and use that folder as the personal library location. Let's say that you installed both the assert and assertions package, as we did above.

To use assert, you would simply run R, and it will find that package because it was installed in the default library. However, if you tried to use assertions, it would not be found.

$ R --quiet
> library(assert)
> library(assertions)
Error in library(assertions) : there is no package called ‘assertions’

To use assertions, you would need first to set and export the environment variable, which will then make assertions available and assert unavailable.

$ export R_LIBS_USER='~/Rtest'
$ R --quiet
> library(assertions)
> library(assert)

Installing from Bioconductor

Some packages will be available only from Bioconductor, and to install the Bioconductor package manager, which is then used to install the Bioconductor packages. The package manager can be installed with

> install.packages("BiocManager")

Once the BiocManager package is installed, you would typically use the BiocManager::install() to install Bioconductor packages. BiocManager may not use the default value of R_LIBS_SITE automatically, and if you find that it does not, you have two options if you wanted to install the package abind:

  • Explicitly export it using
$ export R_LIBS_USER="~/R/x86_64-pc-linux-gnu-library/4.4"
$ R
> library(BiocManager)
> BiocManager::install('abind')

or

  • Specify the library folder on the BiocManager::install() command.
$ R
> library(BiocManager)
> BiocManager::install('abind', lib="~/R/x86_64-pc-linux-gnu-library/4.4")

Please write us at vacchelp@uvm.edu if you have questions about installing or managing your own R library collections.