This document walks you through the steps necessary to install R packages in your HOME so you do not need root access.
R comes with a single library $R_HOME/library
which
contains the standard and recommended packages. This is usually in a
system location where you cannot install your libraries. R also has a
default value for a directory where users can install their own R
packages. On workstations, we suggest using ~/local/R_libs/<R_version>
so that you tag your package folder with the specific version of R you are using to install them. The tagging is useful so that you do not risk in the future to forget and accidentally use the packages with a different version of R. This directory doesn't exist by default. The first time you install an R package, you have to create the directory.
The R_LIBS_USER
environment variable is used by R to determine where packages should be located when the install.packages()
function is called. It is set using:
export R_LIBS_USER=~/local/R_libs/<R_version>:$R_LIBS_USER
Note: You can also add this to your ~/.bashrc
file, but we recommend calling this directly after loading the module in your scripts or when running R interactively. This ensures that your local library is the first one checked by R for installs and libraries.
Let's create the library directory (has to be done just once for each R version you use):
mkdir -p ~/local/R_libs/3.6.3
Optional - You need to load the module for R if you do not use the system R
module load r/3.6.3
Setup R_LIBS_USER environment variable:
export R_LIBS_USER=~/local/R_libs/3.6.3:$R_LIBS_USER
Then fire up an R session:
R
To install package lattice, use this command inside R:
> install.packages("lattice", repos="http://cran.r-project.org")
It gives the following message:
Installing package into ‘/home/arenaam/local/R_libs/3.6.3’
(as ‘lib’ is unspecified)
trying URL 'http://cran.r-project.org/src/contrib/lattice_0.20-41.tar.gz'
Content type 'application/x-gzip' length 389631 bytes (380 KB)
==================================================
downloaded 380 KB
* installing *source* package ‘lattice’ ...
** package ‘lattice’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
gcc -std=gnu99 -I"/usr/share/R/include" -DNDEBUG -fpic -g -O2 -fdebug-prefix-map=/build/r-base-V28x5H/r-base-3.6.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c init.c -o init.o
gcc -std=gnu99 -I"/usr/share/R/include" -DNDEBUG -fpic -g -O2 -fdebug-prefix-map=/build/r-base-V28x5H/r-base-3.6.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c threeDplot.c -o threeDplot.o
gcc -std=gnu99 -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o lattice.so init.o threeDplot.o -L/usr/lib/R/lib -lR
installing to /home/arenaam/local/R_libs/3.6.3/00LOCK-lattice/00new/lattice/libs
** R
** data
*** moving datasets to lazyload DB
** demo
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (lattice)
The downloaded source packages are in
‘/tmp/RtmpjFHPxp/downloaded_packages’
If you want to use another location rather than the default location, for example, /userapps/<YOUR_GROUP>/share/R_libs/<R_version>
, you need to create the directory first:
mkdir -p /userapps/<YOUR_GROUP>/share/R_libs/<R_version>
Setup R_LIBS_USER environment variable:
export R_LIBS_USER=/userapps/<YOUR_GROUP>/share/R_libs/<R_version>:$R_LIBS_USER
Then type the following command inside R (after loading it):
> install.packages("lattice", repos="http://cran.r-project.org", lib="/userapps/<YOUR_GROUP>/share/R_libs/<R_version>")
It is a bit of burden having to type the long string of library path every time. To avoid doing that, you can create a file .Renviron
in your home directory, and add the following line to the file:
export R_LIBS_USER=/userapps/<YOUR_GROUP>/share/R_libs/<R_version>
You can use multiple paths separated by :
(colon). Whenever R starts, the directory /userapps/<YOUR_GROUP>/share/R_libs/<R_version>
is added to the list of places to look for R packages and so:
> install.packages("lattice", repos="http://cran.r-project.org")
will have the same effect as the previous install.packages()
command.
To see the directories where R searches for libraries, use the command:
> .libPaths()
When you install an R package, you are asked which repository R should
use. To set the repository and avoid having to specify this at every
package install, create a file .Rprofile
in your home directory. This is the start up code for R. Add the following line to the file:
cat(".Rprofile: Setting R repository:")
repo = getOption("repos")
# set up the server from which you will download the package.
repo["CRAN"] = "http://cran.case.edu"
options(repos = repo)
rm(repo)
Now you only need to do
> install.packages("lattice")
That will download the package lattice from http://cran.case.edu
and install it in ~/local/R_libs/<R_version>
.
> update.packages()
inside an R session is the simplest way to ensure that all the packages in your local R library are up to date. It downloads the list of available packages and their current versions, compares it with those installed and offers to fetch
and install any that have later versions on the repositories.
> remove.packages(“lattice”)
inside an R session to remove package lattice
. An even easier way is just to go into the directory ~/local/R_libs/<R_version>
and remove the directory lattice
from there.
Add-on packages in R installation guide