Site icon Haktan Suren, PhD

Installing R package from the source

First of all, I highly recommend checking my other post if you are getting package ‘xxx’ is not available (for R version x.y.z)” warning.

You are still with me? OK, let’s continue.

First of all, you need to install the package. Find your package you want to install on cran-r website.

Usually, you can find the tar balled source file on package’s page (highlighted on the image below).

Simply right click and copy the link address. And do this in your shell.

wget https://cran.r-project.org/src/contrib/your-package.tar.gz

Once it is downloaded, you need to untar with the following command.

tar -zxvf your-package.tar.gz

This will unpack the content of the tar ball, and place it in the directory you are in.

If it is a properly packed tar, you’ll find your R functions in package-name/R folder.

Now you can simply source the content of the R folder, I have a code to read and source the content of R recursively. I remember I found this code online very long time ago. So credit goes to “Anonymous”

run R on your shell, or open R studio. And copy/paste the code below.

 
sourceDir <- function(path, trace = TRUE, ...) {
    for (nm in list.files(path, pattern = "\\.[RrSsQq]$")) {
       if(trace) cat(nm,":")           
       source(file.path(path, nm), ...)
       if(trace) cat("\n")
    }
}

And then,

sourceDir('your-package/R')

This will automatically read the content of R folders and source the individual files recursively for you.

A word of caution, If you downloaded a package that requires compiling first (e.g. written in C++), those steps might not be able to work. I will try to address this issue later on.

Feel free to comment your experience or problem below. I would love to read and answer them all.

Exit mobile version