Installing R Packages

The R console, Built-in Apps and Shiny Apps come with a number of the most commonly used data science packages pre-installed (e.g. ggplot2, dplyr, RPostgreSQL, etc). To get the best performance you should only ever install these specific packages manually if they explicitly require a different version to the one that is baked in. To see the full list of pre-installed packages, please visit this article.

The built-in R console allows users to easily install packages from the CRAN repository. The list of packages available for installation is updated on a weekly basis. It is also possible to install packages not available in CRAN though the process is a bit more complicated as the package first must be downloaded to the user machine and then uploaded to the workspace manually.

General installation guidelines

The R packages installed from the R console, Built-in Apps and Shiny Apps will be installed to a persistent location within the workspace. Therefore, you only need to install a package once, as all the other scripts and Shiny Apps that use the same version of R will be able to access it.

The packages installed by workspace users can be found in files/R/<R_version> and this location is included in the libPaths of the R console, so users can load them using the following command:

library(package_name)

To list all the installed packages from within the R console run:

installed.packages()

To check whether a particular package is installed:

find.package('package_name')

Installing R packages from CRAN

To install a package from a CRAN repository:

install.packages(‘package_name’)

Since CRAN is a network of servers, known as mirrors, you can specify which mirror you would like to install the package from. Before selecting a mirror, check that it is on the allowed domains list in your workspace.

To install a package from a specific CRAN mirror:

install.packages('readr', repos='mirror_url')

In order to avoid lengthy, unnecessary installations we recommend that install.packages() should not be directly run within scripts unless the package is not already installed. This can either be done by setting up the desired library of R packages beforehand or by including code to only install the package if required. The following command will only install a package if it cannot already be loaded:

xaputils::xap.require('package')

More detailed information on package installation are available from the R manuals at CRAN.

install_mran.gif

Installing R packages not hosted on CRAN

Because Internet access in the workspace is restricted, you will need to download the package that you want to install to your local machine first, and then upload it to the workspace. Make sure that dependencies are available on CRAN or you will have to upload them manually as well.

In this example, we will show you how to install the ggman package used for creating Manhattan plots.

First, navigate to the ggman GitHub repository, and download the package as a ZIP file.

download_zip_github.png

Then, upload the ZIP file to the workspace folder of the R version in which you intend to install the package. In this example, we upload the ZIP file to the ~/files/R/4.0.2/ directory.

upload_ggman.png

Next, open the R console, and install the devtools package:

install.packages('devtools')

Then, load the devtools library:

library(devtools)

Install the package:

install_local("~/files/R/4.0.2/ggman-master.zip")

Test the installation:

library(ggman)

If the installation fails, it may be because the packages has dependencies that are not hosted on CRAN either. To find out what dependencies are missing, run the install command again but without installing any dependencies:

install_local("~/files/R/4.0.2/ggman-master.zip", dependencies=FALSE)

This will prompt all the dependencies that need to be installed. If they are hosted on CRAN, you can use install.packages(), otherwise you will need to upload the ZIP file of that package to the workspace and install it manually as shown above.

Package installation issues

In some circumstances, packages may fail to install. There are several possible reasons for this:

  • The necessary system requirements are not available.
  • The package is incompatible with the version of R running in the console. If you require access to a specific package that fails to install please contact the Service Desk.
Updated on August 31, 2023

Was this article helpful?