Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install R packages using docker file

I have installed R using below line in my docker file. Please suggest how do I specify now packages to be installed in my docker file.

RUN yum -y install R-core R-devel 

I'm doing something like this:

RUN R -e "install.packages('methods',dependencies=TRUE, repos='http://cran.rstudio.com/')"\     && R -e "install.packages('jsonlite',dependencies=TRUE, repos='http://cran.rstudio.com/')" \     && R -e "install.packages('tseries',dependencies=TRUE, repos='http://cran.rstudio.com/')"  

Is this the right way to do?

like image 472
Ashag Avatar asked Jul 24 '17 20:07

Ashag


People also ask

How do I manually install an R package?

Go into R, click on Packages (at the top of the R console), then click on "Install package(s) from local zip files", then find the zip file with arm from wherever you just saved it. Do the same thing to install each of the other packages you want to install.

What is Docker for RStudio?

RStudio R Docker Images. RStudio creates and distributes an opinionated set of R binaries for different Linux distributions. These Docker images are built to use those R binaries. The images are intentionally minimal, their primary purpose is to serve as the basis for other images requiring R.


2 Answers

As suggested by @Cameron Kerr's comment, Rscript does not give you a build failure. As of now, the recommended way is to do as the question suggests.

RUN R -e "install.packages('methods',dependencies=TRUE, repos='http://cran.rstudio.com/')" RUN R -e "install.packages('jsonlite',dependencies=TRUE, repos='http://cran.rstudio.com/')" RUN R -e "install.packages('tseries',dependencies=TRUE, repos='http://cran.rstudio.com/')"  

If you're fairly certain of no package failures then use this one-liner -

RUN R -e "install.packages(c('methods', 'jsonlite', 'tseries'),                            dependencies=TRUE,                             repos='http://cran.rstudio.com/')" 

EDIT: If you're don't use the Base-R image, you can use rocker-org's r-ver or r-studio or tidyverse images. Here's the repo. Here's an example Dockerfile -

FROM rocker/tidyverse:latest  # Install R packages RUN install2.r --error \     methods \     jsonlite \     tseries 

The --error flag is optional, it makes install.packages() throw an error if the package installation fails (which will cause the docker build command to fail). By default, install.packages() only throws a warning, which means that a Dockerfile can build successfully even if it has failed to install the package.

All rocker-org's basically installs the littler package for the install2.R functionality

like image 171
Ic3fr0g Avatar answered Oct 02 '22 05:10

Ic3fr0g


Yes, your solution should work. I came across the same problem and found the solution here https://github.com/glamp/r-docker/blob/master/Dockerfile.

In short, use: RUN Rscript -e "install.packages('PACKAGENAME')". I have tried it and it works.

As others have mentioned in the comments, this solution will not raise an error if the package could not be installed.

like image 23
elevendollar Avatar answered Oct 02 '22 05:10

elevendollar