Installing R packages yourself.¶
This page is about 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 and bundles like R-bundle-bioconductor, R-bundle-CRAN, and R-bundle-CRAN-extra has most of the packages available from CRAN. If you need more than those, the quickest and sometimes best solution is to install those add-ons on your own account. Otherwise ask support to have them installed (and usually then added to the R-bundle-CRAN-extra module).
Preparations¶
We need to 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:
- R reads the
$HOME/.Renvironfile to setup its environment. It should be created by R on first run, or you can create it with the command: touch $HOME/.Renviron - 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.
- Since the environment file probably is empty now, tell R where your chosen add-on directory is with the command line:
- However if it is not empty, you can edit
$HOME/.Renvironwith your favorite editor so thatR_LIBScontain the path to your chosen add-on directory. It should look something like this when you are done:
- NOTE: Replace “
/home/u/user” with the value of$HOME. Run ‘echo $HOME’ to see its value. - NOTE: The
%Vshould be written as-is, it is substituted at runtime with the active R version.
- NOTE: Replace “
- For each version of R you are using, create a directory matching the pattern used in
.Renvironto store your packages in. This example is shown for R version 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¶
Example
In this example we use the plyr add-on, mostly because it has a dependency (Rcpp).
- Load the R module first. This example loads the R version 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.
You get a warning about ‘lib’ being unspecified. You can safely ignore that.
Note
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 the manual method.
Manual download and install¶
- Download the add-on of interest from the CRAN Package site.
- Install from inside R with
R CMD INSTALL -l
Example
- In this case we download http://cran.r-project.org/src/contrib/ash_1.0-9.tar.gz (it has no dependencies):
- Load the R module. Here we use R version 3.5.1:
- Tell R to install it into your chosen add-on directory:
Using your package¶
To use your installed add-ons, start R and just use the following R expression to load the plyr add-on (replacing “plyr” with your add-on):
Packages not on R CRAN¶
HPC2N has the vast majority of packages from R CRAN installed, as well as many of the packages from Bioconductor. Asking for one that is not installed will usually mean we install it.
However, if you want to install a package yourself from Bioconductor you first need to install the BiocManager package. This is done from inside R:
# install the BiocManager from CRAN using the base R install.packages() function
install.packages("BiocManager")
You can check if it works with this command:
You can now install Bioconductor packages (
Other places that can have useful R packages are GitHub etc. You generally need to install devtools from CRAN first, and then possibly other dependencies.
and then install the package
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?.