Installing R packages

The R console and Mini-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 or a Mini-app 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 Mini-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 whitelisted 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:

xap.require('package')

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

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 ggman package used for creating Manhattan plots.

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

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 ~/files/R/4.0.2/ directory.

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)

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 June 21, 2021

Was this article helpful?

Related Articles

Not the solution you were looking for?
Click the link below to submit a support ticket
CONTACT SERVICE DESK