446

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

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

I'm running Mac OS X.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
  • 1
    This is very closely related: http://stackoverflow.com/questions/11105131/cannot-install-r-forge-package-using-install-packages – GSee Dec 07 '12 at 18:23

6 Answers6

567

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".
Shane
  • 95,736
  • 34
  • 221
  • 217
109

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

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
rcs
  • 64,778
  • 22
  • 167
  • 150
  • 3
    I tried this and got an error: Madjoro-MacBook-Pro:~ Madjoro$ R CMD INSTALL RJSONIO_0.2-3.tar.gz Warning: invalid package ‘RJSONIO_0.2-3.tar.gz’ Error: ERROR: no packages specified –  Sep 24 '09 at 22:51
  • 1
    You have to specify the correct path to the .tar.gz file and the XCode tools (http://developer.apple.com/TOOLS/Xcode/) are required. – rcs Sep 26 '09 at 21:27
  • 1
    Is there a way to build the binary .zip from the source? – haridsv Jan 29 '10 at 00:23
  • 6
    Found the solution, you need to use --binary option. – haridsv Jan 29 '10 at 00:29
  • 1
    How many times have you come here because you typed install instead of INSTALL? – Brandon Bertelsen May 01 '20 at 18:15
53

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

install.packages("RJSONIO", repos = "http://www.omegahat.org/R", type="source")
Eduardo Leoni
  • 9,044
  • 6
  • 40
  • 47
  • 3
    I tried this and got an error: * Installing *source* package ‘RJSONIO’ ... ** libs ** arch - i386 sh: make: command not found ERROR: compilation failed for package ‘RJSONIO’ RMate stopped at line 3 * Removing ‘/Library/Frameworks/R.framework/Versions/2.9/Resources/library/RJSONIO’ The downloaded packages are in ‘/private/var/folders/Ey/EyzhYjoKESmsmsZ6K87PeU+++TI/-Tmp-/Rtmpe3C96p/downloaded_packages’ Updating HTML index of packages in '.Library' Warning message: In install.packages("RJSONIO", repos = "http://www.omegahat.org/R", : installation of package 'RJSONIO' had non-zero exit status –  Sep 24 '09 at 22:48
  • 6
    Do you have the developer tools installed? They come in the Mac OS X installation dvd. Since this package has C code you will need a compiler to install it from source. – Eduardo Leoni Sep 24 '09 at 23:11
  • 1
    I suspect I do not have the developer tools installed. Atleast, I don't remember installing them. Thanks! –  Sep 24 '09 at 23:26
  • 4
    If you are doing this on windows, you can get the developer tools from http://www.murdoch-sutherland.com/Rtools/ ... make sure when installing you check the box that says to update your path (may be a bit hard to read .... just checked the unchecked box that comes up) – Dan Goldstein Oct 06 '09 at 15:21
32

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

ReneWang
  • 496
  • 5
  • 7
11

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.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Dodgie
  • 632
  • 10
  • 17
9

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

R CMD build --binary RJSONIO_0.2-3.tar.gz
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
haridsv
  • 8,079
  • 4
  • 58
  • 63