Skip to content

Installing R Packages Locally

Work In Progress

This article is a work in progress. Chances are, information is either incomplete or just plain missing.

Prepare Your Account

You need to create a new directory specifically for R package installation. We recommend creating a directory in your home directory called R_libs, like:

[barney@hpc ~]$ mkdir ~/R_libs

CRAN: The Comprehensive R Archive Network

CRAN is a network of FTP and web servers around the world that store identical, up-to-date versions of code and documentation for R. It provides access to both the official source code of the R software, and tens of thousands of R packages.

Installing R Packages via CRAN

Load the appropriate module for the R version you plan to use. In this case, we'll use R/4.0.3. To load R and then run the R console on the HPC, run the following commands on the login node:

[barney@hpc ~]$ module load R/4.0.3
[barney@hpc ~]$ R

Once you're in the R console, you'll need to use the function install.packages to download and install the desired packages. For example, to load the remeta package, you could run the following code:

install.packages("remeta", repos="http://cran.us.r-project.org", lib="~/R_libs/")

The first parameter is the package name we want to install, in this case, remeta. The second parameter, repos, is the location we want to download the package from (most everything is downloaded from http://cran.us.r-project.org). The third parameter is the location of the local library directory you created above.

To use the package from R, we use the library function. By default, library only searches the system directories, so we have to specify our local R_libs directory:

library("remeta", lib="~/R_libs/")

To quit the R console, enter the command q().

Installing R Packages without CRAN

If the R packages are unavailable on CRAN or you want to install an older version of the packages, you can download the compressed file to your home directory and install it. In this section, we'll use ggplot2 version 2.0.0 as an example.

It is helpful to cd into your new R_libs directory for R package installation and load the R module:

[barney@hpc ~]$ cd ~/R_libs
[barney@hpc ~/R_libs]$ module load R/4.0.3

Since we know the URL, the command wget can be used to download the .tar.gz file. You can also copy the compressed file to the directory if you have the file on your local machine or somewhere else on the server.

[barney@hpc ~/R_libs]$ wget http://cran.r-project.org/src/contrib/Archive/ggplot2/ggplot2_2.0.0.tar.gz

Now, to install the package manually, we need to run the following command:

[barney@hpc ~/R_libs]$ R CMD INSTALL --library=~/R_libs/ ggplot2_2.0.0.tar.gz

Open the R console and use the library function to load the newly installed library:

library("ggplot2", lib="~/R_libs/")

Acknowledgments

Content for this document was borrowed from the UCONN web page https://bioinformatics.uconn.edu/resources-and-events/tutorials-2/install-python-r-packages-in-users-home-directory/# .