Installing Local R packages

Before You Start

To install an R package, first we will reserve a compute node:

srun --mem=4gb --pty /bin/bash

Then load R:

module swap intel gcc

module load R

NOTE: 

Install packages with the R prompt

Once R is loaded, run R and then install the package you want with

install.packages("package_name")

For this example, we will install ggplot2:

install.packages("ggplot2")

You can observe an output similar to this one:

Installing package into ‘/home/dxb507/R/x86_64-pc-linux-gnu-library/3.3’ 

(as ‘lib’ is unspecified)

--- Please select a CRAN mirror for use in this session ---

Warning: failed to download mirrors file (cannot download all files); using local file '/usr/local/gcc-6_3_0/openmpi-2_0_1/R/3.3.3/lib64/R/doc/CRAN_mirrors.csv'

HTTPS CRAN mirror 

 1: 0-Cloud [https]                 2: Algeria [https]              

 3: Australia (Melbourne) [https]   4: Australia (Perth) [https]    

 5: Austria [https]                 6: Belgium (Ghent) [https]      

 7: Brazil (RJ) [https]             8: Brazil (SP 1) [https]        

 9: Bulgaria [https]               10: Canada (MB) [https]          

11: Chile [https]                  12: China (Beijing) [https]      

13: China (Hefei) [https]          14: Colombia (Cali) [https]      

15: Czech Republic [https]         16: Denmark [https]              

17: France (Lyon 1) [https]        18: France (Lyon 2) [https]      

19: France (Marseille) [https]     20: France (Montpellier) [https] 

21: France (Paris 2) [https]       22: Germany (Falkenstein) [https]

23: Germany (Münster) [https]      24: Iceland [https]              

25: India [https]                  26: Ireland [https]              

27: Italy (Padua) [https]          28: Japan (Tokyo) [https]        

29: Malaysia [https]               30: Mexico (Mexico City) [https] 

31: New Zealand [https]            32: Norway [https]               

33: Philippines [https]            34: Russia (Moscow) [https]      

35: Serbia [https]                 36: Spain (A Coruña) [https]     

37: Spain (Madrid) [https]         38: Switzerland [https]          

39: Taiwan (Chungli) [https]       40: Turkey (Denizli) [https]     

41: UK (Bristol) [https]           42: UK (Cambridge) [https]       

43: UK (London 1) [https]          44: USA (CA 1) [https]           

45: USA (IA) [https]               46: USA (KS) [https]             

47: USA (MD) [https]               48: USA (MI 1) [https]           

49: USA (TN) [https]               50: USA (TX) [https]             

51: USA (WA) [https]               52: (HTTP mirrors)               

Selection: 

Select one mirror (we recommend using https), and just wait for the package to install. If the installation is successful, you should get an output similar to this one:

trying URL 'https://mirror.las.iastate.edu/CRAN/src/contrib/ggplot2_2.2.1.tar.gz'

Content type 'application/x-gzip' length 2213308 bytes (2.1 MB)

==================================================

downloaded 2.1 MB

* installing *source* package ‘ggplot2’ ...

** package ‘ggplot2’ successfully unpacked and MD5 sums checked

** R

** data

*** moving datasets to lazyload DB

** inst

** preparing package for lazy loading

** help

*** installing help indices

** building package indices

** installing vignettes

** testing if installed package can be loaded

* DONE (ggplot2)

Since you do not have permission to install in the R path, the package will be installed in your home directory by default. For more information, visit HPC Software Installation Guide.

Installing Packages From the Command Line

This kind of installation is useful when we want to install a particular package that is not in CRAN.

Download a package into a directory. For this example we will download and install ggplot2 from the command line.

wget https://cran.r-project.org/src/contrib/ggplot2_2.2.1.tar.gz 

Then, once it is downloaded, we install it with the following command:

R CMD INSTALL ggplot2_2.2.1.tar.gz 

The output should be exactly the same as before:

* installing to library ‘/home/dxb507/R/x86_64-pc-linux-gnu-library/3.3’

* installing *source* package ‘ggplot2’ ...

** package ‘ggplot2’ successfully unpacked and MD5 sums checked

** R

** data

*** moving datasets to lazyload DB

** inst

** preparing package for lazy loading

** help

*** installing help indices

** building package indices

** installing vignettes

** testing if installed package can be loaded

* DONE (ggplot2)

Advanced Topics

Troubleshooting install errors

Some packages will need help finding header files. In that case, at the R prompt, set the CPATH variable:

Sys.setenv(CPATH = "/usr/local/easybuild_allnodes/software/cairo/2.17.4-GCCcore-11.3.0/include/cairo" )

 If necessary, concatinate paths using the ':', e.g.  CPATH = "<path1>:<path2>:<path3>"

Changing installation directory

You can change the location at any time by setting up the variable R_LIBS_USER 

For example, we could set up the variable to $HOME/.usr/local/R-packages/3.3 with the following command:

export R_LIBS_USER=$HOME/.usr/local/R-packages/3.3

Then run R and install the packages in the new location.

You will have to set up the variable every time you want to use the libraries from the different location.

NOTE: if you do not have a specific use case that requires installing to a custom location, we recommend using the default path.

Uninstalling packages

You can uninstall a local package with either one of the following commands. In this example, the package was installed in R's default location in the user's home. For the example, we will use ggplot2:

Passing Arguments to the Compiler

Some packages will require you to specify a directory containing extra libraries. As before there are two ways to pass these extra arguments:

install.packages("pkgname",

                  configure.args = 

                  c("--option-1=value1", 

                    "--option-2=value2", 

                    "--option-3=value3"))