How do I install an R package from source?

2018-12-31 05:36发布

A friend sent me along this great tutorial on webscraping NYtimes with R. I would really love to try it. However, the first step is to installed 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 OSX.

7条回答
与风俱净
2楼-- · 2018-12-31 06:09

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

install.packages("RJSONIO", repos = "http://www.omegahat.org/R", type="source")
查看更多
孤独寂梦人
3楼-- · 2018-12-31 06:18

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

查看更多
其实,你不懂
4楼-- · 2018-12-31 06:18

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

R CMD build --binary RJSONIO_0.2-3.tar.gz
查看更多
裙下三千臣
5楼-- · 2018-12-31 06:21

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".
查看更多
残风、尘缘若梦
6楼-- · 2018-12-31 06:24

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

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

does the trick.

查看更多
呛了眼睛熬了心
7楼-- · 2018-12-31 06:24

I prefer installing a package from R cran project. I will search for the package name and if it is available I will execute the command from my R shell to install it directly from the R cran project. Your package is available in R directory. So this is what I will do

install.packages("RJSONIO")

Bonus - Loading a package into the current session of R

library(RJSONIO)
查看更多
登录 后发表回答