Installing a R package on the clusters

To install a R package, you need to use the install.packages() command. By default, it will try to save the installed package in the global library where a regular (non-root) user cannot write.

Using a local library

As R notes that it cannot write, it will ask whether you want to create a local library. Simply answer ‘y’ to that question to get started.

dfr@hmem00:~ $ R

R version 2.13.1 (2011-07-08)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-redhat-linux-gnu (64-bit)

The .libPaths() command lists the places where R will search for libraries, and use the first item of the list as target for new package installs.

> .libPaths()
[1] "/usr/lib64/R/library" "/usr/share/R/library"

Let’s install the dummy package

> install.packages('dummy')

R tries to install it in the global library

Installing package(s) into ‘/usr/lib64/R/library’
(as ‘lib’ is unspecified)
Warning in install.packages("dummy") :
  'lib = "/usr/lib64/R/library"' is not writable

but quickly notes that it cannot write in the global place, and asks whether it should create a local library. Simply answer ‘y’.

Would you like to create a personal library
to install packages into?  (y/n) y

and the installation process resumes as usual.

--- Please select a CRAN mirror for use in this session ---
Loading Tcl/Tk interface ... done
trying URL ''
Content type 'application/x-gzip' length 3710 bytes
opened URL
downloaded 3710 bytes

* installing *source* package ‘dummy’ ...
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
  converting help for package ‘dummy’
    finding HTML links ... done
    categories                              html
    dummy                                   html
    dummyNews                               html
** building package indices ...
** testing if installed package can be loaded

* DONE (dummy)

The downloaded packages are in
> q()
Save workspace image? [y/n/c]: n

After that, the new package is installed in the user’s home directory:

dfr@hmem00:~ $ ls R/x86_64-redhat-linux-gnu-library/2.13/dummy/

Note that R saves the configuration for future uses:

dfr@hmem00:~ $ R


> .libPaths()
[1] "/home/users/d/f/dfr/R/x86_64-redhat-linux-gnu-library/2.13"
[2] "/usr/lib64/R/library"
[3] "/usr/share/R/library"

The libPaths() command now lists the home directory as the first item. Therefore, subsequently installed packages will be saved in the home directory.

> library(dummy)
dummy 0.1.3
> install.packages('test')
Installing package(s) into ‘/home/users/d/f/dfr/R/x86_64-redhat-linux-gnu-library/2.13’
(as ‘lib’ is unspecified)
--- Please select a CRAN mirror for use in this session ---
Loading Tcl/Tk interface ... done

Final remarks

If the package you want to install has operating system-level dependencies (i.e. the package depends on core libraries), then you will need to ask the system administrator to install the package.

If you feel that the package you want to install is useful for many users, you can also ask a system administrator to install it globally.

Finally, note that packages depend on the R version you are using. Make sure to load the right module before you install a package!