About R libraries

R is basically a programming language, and it comes with a set of libraries, often called packages, preinstalled by default. Those are considered the ‘base’ packages. There are thousands of other packages written for R, and many of them are available from the Comprehensive R Archive Network (CRAN).

R has built-in capability to contact CRAN, download packages from there, and install them quite simply. You install R packages (or libraries or modules) only once from a login node and they are available to load thereafter.

Comprehensive R Archive Network

R libraries are obtained from the Comprehensive R Archive Network (CRAN), and at CRAN will have a reference manual available. If you go to the CRAN web site, http://cran.r-project.org/, then follow the link on the left under Software to Packages, there is a Table of available packages, sorted by name. So, for example, the package agricolae, has on its package page at CRAN both a reference manual and a vignette, both in .pdf format. R vignettes are typically examples or tutorials and can be quite helpful. You should check to see if your package has one.

Most R packages will install nicely and without difficulty. It will usually be a matter of running the install.packages() command with the appropriate package name and making a couple of choices. Most problems arise because of version incompatibilities. In some cases, an older library will have been superceded by a newer package, or it may have changed name for a newer version if substantial changes have been made.

Setting the Library Location

R maintains a list of folders that contain libraries. The libraries are typically maintained at three organizational levels. The highest is at the installation level, and on Flux, that is done by the administrators. The next level is called the site, and on Flux that is typically configured by the support staff in the colleges, schools, or departments. Libraries maintained by them are also typically made available via a loadable software module. For example, the people who support the Michigan Medicine provide an r-biomed-libs module that contains the location of libraries they’ve found are useful for biomedical researchers.

The last level is the user level, which is where you take control. You can install your own libraries, and those libraries can override those maintained at the installation or the site levels.

There are several ways to specify that you have a personal library collection, but probably the best way, and one that should work on Linux, Mac, or Windows, is to create a .Renviron file, and put the library location there. That will add the library to your R environment everytime you start R.

The one catch to this is that the library is specific to a major version of R. So, libraries installed by R/3.3.0 will typically also be OK to use with R/3.3.1, or any other version that begins with 3.3. However, they may not work with R/3.4.0.

With customization comes some responsibility and the requirement for deeper knowledge of how things work.

Given that, if you will be working with the same major version of R, let’s use 3.4 as our major version number but substitute your own if you will use something different, then you might want to create a folder for those libraries called ~/Rlibrary/3.4.

$ mkdir -p ~/Rlibrary/3.4

Then, in your home directory create a text file called .Renviron, and add to it this text.

## My R libraries for R 3.4.x
R_LIBS_USER=~/Rlibrary/3.4

If you then choose to install a library, that will be its installed location, and it will take precedence over other libraries.

An example installation

Here is an example using the pln package for “Polytomous logit-normit (graded logistic) model estimation”. This example assumes that pln is not already installed, and that you have not set up a personal library.

This is a slightly edited transcript of what it looks like to install and then load a package.

$ R
R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows"
Copyright (C) 2012 The R Foundation for Statistical Computing
  [ . . . . ]
> install.packages("pln")
Warning in install.packages("pln") :
  'lib = "/home/software/rhel6/R/2.15.1-gcc/lib64/R/library"' is not writable
Would you like to use a personal library instead?  (y/n) y
Would you like to create a personal library
~/R/x86_64-unknown-linux-gnu-library/2.15
to install packages into?  (y/n) y
--- Please select a CRAN mirror for use in this session ---
  [ . . . . ]
75: USA (MD)                 76: USA (MI)               
77: USA (MO)                 78: USA (OH)               

Selection: 76
trying URL 'http://cran.mtu.edu/src/contrib/pln_0.2.tar.gz'
Content type 'application/x-gzip' length 88763 bytes (86 Kb)
opened URL
==================================================
downloaded 86 Kb

* installing *source* package 'pln' ...
** package 'pln' successfully unpacked and MD5 sums checked
** libs
gcc -std=gnu99 -I/home/software/rhel6/R/2.15.1-gcc/lib64/R/include
 -DNDEBUG  -I/usr/local/include   -DR -DALL -fpic  -O3
 -mtune=native  -c amatrix.c -o amatrix.o
  [ . . . . ]
installing to /home/uniqname/R/x86_64-unknown-linux-gnu-library/2.15/pln/libs
** R
** data
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded

* DONE (pln)

The downloaded source packages are in
	'/tmp/RtmpnCsqcs/downloaded_packages'
> library(pln)

A couple of comments on the output above. R warns you that you do not have permission to write to the system library and offers you the opportunity to create a personal library

You do want to do this, and you are generally safe to accept the default location for that, which will be in your home directory under R followed by directories to indicate the type of computer, the R version, and package name. If the library into which you are installing exists, you will not be prompted for this information.

Many of the packages need to be compiled, but that should be all taken care of for you by the R install.packages() function.