User Installed Software - R/CRAN

Installing R/CRAN add-ons in your user account

R is a free software environment for statistical computing and graphics. There exists a large number of R add-on packages. The R modules at HPC2N have around 750 of the most common installed and several more in R-bundle-Bioconductor (see our main R page). If you need more than those, the quickest and sometimes best solution is to install those add-ons on your own account.

Preparations

We need create a place for the add-ons to be and tell R where to find them. The initial setup only needs to be done once, but separate package directories need to be created for each R version used:

  1. R reads the $HOME/.Renviron file to setup its environment. It should be created by R on first run, or you can create it with the command: touch $HOME/.Renviron
  2. NOTE: In this example we are going to assume you have chosen to place the R packages in a directory under your home directory. You will need separate ones for each R version.
  3. Since the environment file probably is empty now, tell R where your chosen add-on directory is with the command line:
    echo R_LIBS_USER=\"$HOME/R-packages-%V\" > ~/.Renviron

    However if it is not empty, you can edit $HOME/.Renviron with your favorite editor so that R_LIBS contain the path to your chosen add-on directory. It should look something like this when you are done:

    R_LIBS_USER="/home/u/user/R-packages-%V"
    

    NOTE: Replace "/home/u/user" with the value of $HOME. Run 'echo $HOME' to see its value.
    NOTE: The %V should be written as-is, it's substituted at runtime with the active R version.

  4. For each version of R you are using, create a directory matching the pattern used in .Renviron to store your packages in. This example is shown for R version 3.6.0:

    mkdir -p $HOME/R-packages-3.6.0

Installing R add-ons

There are two ways to install an R add-on from CRAN, which is not installed on our system. You can either choose the one that automatically downloads the add-on and handles all the dependencies, or one that is somewhat simpler and does not handle dependencies.

Automatic download and install

In this example we use the plyr add-on, mostly because it has a dependency (Rcpp).

  1. Load the R module first. This example loads the R version 3.5.1:
    ml GCC/7.3.0-2.30  OpenMPI/3.1.1 R/3.5.1

    Tell R to install the plyr add-on from the CRAN repo in Sweden (chosen from CRAN mirror list). We ask R to be quiet and don't bother saving and restoring the environment.

    R --quiet --no-save --no-restore -e "install.packages('plyr', repos='http://ftp.acc.umu.se/mirror/CRAN/')"

You get a warning about 'lib' being unspecified. You can safely ignore that.

If the package has dependencies that come from more than one repo it will not work. You either run the "install.packages" interactively in R or use method two.

Manual download and install

  1. Download the add-on of interest from the CRAN Package site. In this case we download http://cran.r-project.org/src/contrib/ash_1.0-9.tar.gz (it has no dependencies):
    wget http://cran.r-project.org/src/contrib/ash_1.0-9.tar.gz
  2. Load the R module first. Example uses R version 3.5.1:
    ml GCC/7.3.0-2.30  OpenMPI/3.1.1 R/3.5.1
    
  3. Tell R to install it into your chosen add-on directory:
    R CMD INSTALL -l $HOME/R-packages-3.5.1 ash.tar.gz
    

Using

To use you installed add-ons, just use the following R expression to load the plyr add-on (replacing "plyr" with your add-on):

library("plyr")

More information

For more information about installing and using your own packages see the offical FAQ (http://cran.r-project.org/doc/FAQ/R-FAQ.html), particularly How can add-on packages be installed and How can add-on packages be used?.

Updated: 2024-03-08, 14:54