Installing R package from the source

By Haktan Suren, PhD
Dec 6th, 2015
23 Comments
43501 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, PhD
- Webguru, Programmer, Web developer, and Father :)

23 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?

  4. Olong Johnson says:

    What am I doing wrong? I am trying to install prophet but it doesnt seem to be working. This is how I typed in the code, is there an error in it?

    > ‘https://cran.r-project.org/web/packages/prophet/index.html’
    [1] “https://cran.r-project.org/web/packages/prophet/index.html”
    > ‘tar -zxvf your-package.tar.gz
    + ‘
    [1] “tar -zxvf your-package.tar.gz\n”
    > sourceDir
    > sourceDir(‘prophet’)
    > library(prophet)
    Error in library(prophet) : there is no package called ‘prophet’

  5. Colin says:

    Hi, thank you for this and previous posts.. they have been helpful to me and my colleges. I followed your instructions from the “INSTALLING R PACKAGE: FIXING PACKAGE ‘XXX’ IS NOT AVAILABLE (FOR R VERSION X.Y.Z)” WARNING?” post and ended up here because I was still having an issue. I am not finding the ‘libcurl4-openssl-dev’ for R 3.5.2 on the package list at CRAN. Please let me know if you find a way around this for the most recent R:

    > install.packages(“libcurl4-openssl-dev”,repos=’http://cran.us.r-project.org’)
    Installing package into ‘/home/realseq/R/x86_64-pc-linux-gnu-library/3.5’
    (as ‘lib’ is unspecified)
    Warning in install.packages :
    package ‘libcurl4-openssl-dev’ is not available (for R version 3.5.2)

  6. Akhzar Nazir says:

    package ‘RTextTools’ is not available (for R version 3.5.3)

  7. Wbarker says:

    Having same trouble with quantstrat in R version 3.6.0: Warning in install.packages :
    package ‘quantstrat’ is not available (for R version 3.6.0). Tried all of the above. Any new workarounds?

  8. Fei says:

    > install.packages(“Seurat”)
    Warning in install.packages :
    package ‘Seurat’ is not available (for R version 3.6.2)
    Warning in install.packages :
    unable to access index for repository https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.6:
    cannot open URL ‘https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.6/PACKAGES’

    > install.packages(“Seurat”, repos = ‘http://cran.us.r-project.org’)
    trying URL ‘http://cran.us.r-project.org/bin/macosx/el-capitan/contrib/3.6/Seurat_3.1.2.tgz’
    Content type ‘application/octet-stream’ length 3202077 bytes (3.1 MB)
    ==================================================
    downloaded 3.1 MB

    The downloaded binary packages are in
    /var/folders/zb/5qbgts5n5rs_fj0zfg978zjc0000gn/T//RtmpzjQAnr/downloaded_packages

    Anyone can help me? many thanks

  9. Wonde says:

    I am trying to MCMCglmm package of R. However, I have faced technical error to ordering the pedigree. So far, even though both MasterBayes and pedantics are removed from CRAN, I tried to install the latest archive of both packages but both of them were not integrated to my R v.3.6.1. Software.

    Could you please let me know how to install it.

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