Last updated Mar 26, 2020 11:56 AM

Installing R Packages from Source

While working with R, typically packages are installed using CRAN. However, sometimes you run into an issue where you're using a specific version of R and need an older (or newer, development) version of a package. There isn't a way to instruct CRAN to install an old version. What you can do instead is go to the package's overview page on CRAN, for example: https://cran.r-project.org/web/packages/caTools/index.html, and look for the line that says "Old sources." Click on the archive link, and you should see several versions of the package, like this:

screenshot_cran.png

Unfortunately, there is usually no great way to tell which versions of which package are compatible with which R version (try saying that 10 times fast!). The best way to handle this is to download several versions, starting with the NEWEST. Copy the link location for the newest version of the package you're trying to install, as you will need it for the next step. Login to Fiji however you normally do, and you should be at a command prompt. Use wget to download the source by pasting the link from before, noting which directory you're running the command from, as this is where the file will get downloaded to:

        jode5573@fiji-2:~$ wget https://cran.r-project.org/src/contrib/Archive/caTools/caTools_1.16.tar.gz
--2020-03-26 11:40:25--  https://cran.r-project.org/src/contrib/Archive/caTools/caTools_1.16.tar.gz
Resolving cran.r-project.org (cran.r-project.org)... 137.208.57.37
Connecting to cran.r-project.org (cran.r-project.org)|137.208.57.37|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 63331 (62K) [application/x-gzip]
Saving to: ‘caTools_1.16.tar.gz’

100%[==========================================================================================================================================================>] 63,331       216KB/s   in 0.3s   

2020-03-26 11:40:26 (216 KB/s) - ‘caTools_1.16.tar.gz’ saved [63331/63331]
    

Next, open whichever version of R you are trying to install your package for. If you are using Rstudio-server on Fiji, it will always be the system version, meaning you have not used module load to change your R version. You should just see information about R and a blank prompt:

        jode5573@fiji-2:~$ R

R version 3.5.0 (2018-04-23) -- "Joy in Playing"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-redhat-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

>
    

Lastly, we can still use install.packages() to install from source, we just have to manually specify where the file is. Be sure to change the path and filename accordingly for your use case!

        > install.packages("/Users/jode5573/caTools_1.16.tar.gz", repos = NULL, type="source")
Installing package into ‘/Users/jode5573/R/x86_64-redhat-linux-gnu-library/3.5’
(as ‘lib’ is unspecified)
* installing *source* package ‘caTools’ ...
** package ‘caTools’ successfully unpacked and MD5 sums checked
** libs
g++ -m64  -I"/usr/include/R" -DNDEBUG   -I/usr/local/include   -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c Gif2R.cpp -o Gif2R.o
g++ -m64  -I"/usr/include/R" -DNDEBUG   -I/usr/local/include   -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c GifTools.cpp -o GifTools.o
gcc -m64 -std=gnu99 -I"/usr/include/R" -DNDEBUG   -I/usr/local/include   -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c runfunc.c -o runfunc.o
g++ -m64 -shared -L/usr/lib64/R/lib -Wl,-z,relro -o caTools.so Gif2R.o GifTools.o runfunc.o -L/usr/lib64/R/lib -lR
installing to /Users/jode5573/R/x86_64-redhat-linux-gnu-library/3.5/caTools/libs
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
  converting help for package ‘caTools’
    finding HTML links ... done
    ENVI                                    html  
    GIF                                     html  
    LogitBoost                              html  
    base64                                  html  
    caTools-internal                        html  
    caTools-package                         html  
    colAUC                                  html  
    combs                                   html  
    predict.LogitBoost                      html  
    runmad                                  html  
    runmean                                 html  
    runminmax                               html  
    runquantile                             html  
    runsd                                   html  
    sample.split                            html  
    sumexact                                html  
    trapz                                   html  
** building package indices
** testing if installed package can be loaded
* DONE (caTools)
    

Fiji R
Posted in Category: Cluster Computing