Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R install packages from Shell

I am trying to implement a reducer for Hadoop Streaming using R. However, I need to figure out a way to access certain libraries that are not built in R, dplyr..etc. Based on my research seems like there are two approaches:

(1) In the reducer code, install the required libraries to a temporary folder and they will be disposed when the session is done, like this:

.libPaths(c(.libPaths(), temp <- tempdir()))
install.packages("dplyr", lib=temp, repos='http://cran.us.r-project.org')
library(dplyr)
...

However, this approach will have a dramatic overhead depending on how many libraries you are trying to install. So most of the time will be wasted on installing libraries(sophisticated libraries like dplyr has tons of dependencies which will take minutes to install on a vanilla R session).

So sounds like I need to install it before hand, which leads us to approach2.

(2) My cluster is fairly big. And I have to use some tool like Ansible to make it work. So I prefer to have one Linux shell command to install the library. I have seen R CMD INSTALL... before, however, it feels like will only install packages from source file instead of doing install.packages() in R console, figure out the mirror, pull the source file, install it in one command.

Can anyone show me how to use one command line in shell to non-interactively install a R package? (sorry for this much background knowledge, if anyone thinks I am not even following the right phylosophy, feel free to leave in the comment how this whole cluster R package should be managed.)

like image 445
B.Mr.W. Avatar asked Nov 18 '14 01:11

B.Mr.W.


People also ask

How do you install packages in R?

Open R via your preferred method (icon on desktop, Start Menu, dock, etc.) Click “Packages” in the top menu then click “Install package(s)”. Choose a mirror that is closest to your geographical location. Now you get to choose which packages you want to install.

Which command in R is used to install R packages?

To install any package from CRAN, you use install. packages() . You only need to install packages the first time you use R (or after updating to a new version). **R Tip:** You can just type this into the command line of R to install each package.

How do I install R packages locally?

To install a R package locally, specify the local directory where you want to install by using the “-l” option in the “R CMD INSTALL” command. For example, to install the R package in the local directory “/usr/me/localR/library”, use the “R CMD INSTALL” as follows.


2 Answers

tl;dr

Rscript -e 'install.packages("drat", repos="https://cloud.r-project.org")'

You mentioned you are trying to install dplyr into custom lib location on your disk. Be aware that dplyr package does not support that. You can read more in dplyr#4641.


Moreover if you are installing private package published in internal CRAN-like repository (created by drat or tools::write_PACKAGES), you can easily combine repos argument and resolve dependencies from CRAN automatically.

Rscript -e 'install.packages("priv.pkg", repos=c("cran.priv","https://cloud.r-project.org"))'

This is very handy feature of R repositories, although for production use I would recommend to cache packages from CRAN locally, and use those, so you will never be surprised by a breaking changes in your dependencies. For quality information about handling R in production I suggest to look into talk by Wit Jakuczun at WhyR2019 How to make R great for machine learning in (not only) Enterprise: slides, video.

like image 191
jangorecki Avatar answered Nov 15 '22 20:11

jangorecki


You may find littler useful. It is a command-line front-end / variant of R (which uses the R-embedding interface).

I use the install.r script all the time to install package from the shell. There is a second variant with more command-line argument parsing but it has an added dependency.

like image 24
Dirk Eddelbuettel Avatar answered Nov 15 '22 19:11

Dirk Eddelbuettel