Install R packages using docker file

Ashag picture Ashag · Jul 24, 2017 · Viewed 18.9k times · Source

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?

Answer

Ic3fr0g picture Ic3fr0g · Jan 22, 2019

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 install the littler package for the install2.R functionality