Madjoro
Madjoro

Reputation:

How do I install an R package from source?

A friend sent me along this great tutorial on webscraping The New York Times with R. I would really love to try it. However, the first step is to install a package called [RJSONIO][2] from source.

I know R reasonably well, but I have no idea how to install a package from source.

I'm running macOS (OS X).

Upvotes: 497

Views: 696446

Answers (7)

stevec
stevec

Reputation: 52907

If you have source code you wrote yourself, downloaded (cloned) from GitHub, or otherwise copied or moved to your computer from some other source, a nice simple way to install the package/library is:

In R

It's as simple as:

# install.packages("devtools")
devtools::install('path/to/package')

From terminal

From here, you can clone a GitHub repo and install it with:

git clone https://github.com/user/repo.git
R -e "install.packages('devtools');devtools::install('path/to/package')"

Or if you already have devtools installed, you can skip that first bit and just clone the repo and run:

R -e "devtools::install('path/to/package')"

Note that if you're on ubuntu, install these system libraries before installing devtools (or devtools won't install properly).

apt-get update
apt-get install build-essential libcurl4-gnutls-dev libxml2-dev libssl-dev libfontconfig1-dev libharfbuzz-dev libfribidi-dev libfreetype6-dev libpng-dev libtiff5-dev libjpeg-dev -y

Upvotes: 5

Dodgie
Dodgie

Reputation: 663

From CRAN, you can install directly from a GitHub repository address. So if you want the package at https://github.com/twitter/AnomalyDetection, using

library(devtools)
install_github("twitter/AnomalyDetection")

does the trick.

Upvotes: 12

rcs
rcs

Reputation: 68849

Download the source package, open Terminal.app, navigate to the directory where you currently have the file, and then execute:

R CMD INSTALL RJSONIO_0.2-3.tar.gz

Do note that this will only succeed when either: a) the package does not need compilation or b) the needed system tools for compilation are present. See: R for Mac OS X

Upvotes: 117

haridsv
haridsv

Reputation: 9693

In addition, you can build the binary package using the --binary option.

R CMD build --binary RJSONIO_0.2-3.tar.gz

Upvotes: 9

Shane
Shane

Reputation: 100194

If you have the file locally, then use install.packages() and set the repos=NULL:

install.packages(path_to_file, repos = NULL, type="source")

Where path_to_file would represent the full path and file name:

  • On Windows it will look something like this: "C:\\RJSONIO_0.2-3.tar.gz".
  • On UNIX it will look like this: "/home/blah/RJSONIO_0.2-3.tar.gz".

Upvotes: 626

ReneWang
ReneWang

Reputation: 546

A supplementarily handy (but trivial) tip for installing older version of packages from source.

First, if you call "install.packages", it always installs the latest package from repo. If you want to install the older version of packages, say for compatibility, you can call install.packages("url_to_source", repo=NULL, type="source"). For example:

install.packages("http://cran.r-project.org/src/contrib/Archive/RNetLogo/RNetLogo_0.9-6.tar.gz", repo=NULL, type="source")

Without manually downloading packages to the local disk and switching to the command line or installing from local disk, I found it is very convenient and simplify the call (one-step).

Plus: you can use this trick with devtools library's dev_mode, in order to manage different versions of packages:

Reference: doc devtools

Upvotes: 38

Eduardo Leoni
Eduardo Leoni

Reputation: 9050

You can install directly from the repository (note the type="source"):

install.packages("RJSONIO", repos = "http://www.omegahat.org/R", type="source")

Upvotes: 56

Related Questions