Installing R package from the source

By Haktan Suren
Dec 6th, 2015
9 Comments
4332 Views

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).

package-tar-gz

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.

About the Author

Haktan Suren
-

Webguru, Programmer, Web developer, Graduate student & Father :)

9 Responses to “Installing R package from the source”

  1. Pilar says:

    A-MAZING. I was about to give up

    Thank you!

  2. Deepika Kumawat says:

    Thank you sir. But when I run the command for my package in R
    “tar -zxvf your-package.tar.gz”
    This shows the following error
    Error: unexpected symbol in “tar -zxvf ggplot2_2.2.1.tar.gz”
    Could you please reply that I can I correct it.

  3. Annalise D Souza says:

    Thank you for the post. Any ideas for when the package is out of date, and hence not available on CRAN R?

Wrap your code in <code class="{language}"></code> tags to embed!

Leave a Reply

E-mail address is required for commenting. However, it won't be visible to other users.

Loading Facebook Comments ...
Loading Disqus Comments ...