petergensler
petergensler

Reputation: 342

Verify R packages installed into docker container

Does anyone have any best practices for verifying that R packages are installed into a docker container? I would like to set up my container to run on a CI service, and verify my package are installed, but as I've been building it locally, the logs seem very hard to determine what packages installed, and what did not. It would be nice to either have a CI service do this for me, or to use a simple batch script to verify the packages were installed.

Below is my current dockerfile:

FROM rocker/tidyverse:latest
RUN mkdir -p $HOME/.R
COPY R/Makevars /root/.R/Makevars

RUN apt-get update -qq \
    && apt-get -y --no-install-recommends install \
    liblzma-dev \
    libbz2-dev \
    ed \
    clang  \
    ccache \
    default-jdk \
    default-jre \
    && R CMD javareconf \
    && install2.r --error \
        ggstance ggrepel \
        rstan shinystan rstanarm \
        ###My pkgs
        tidytext janitor corrr officer devtools pacman
        tidyquant timetk tibbletime sweep broom prophet \
        forecast prophet lime sparklyr rsparkling \
        formattable httr rvest xml2 jsonlite \
        textclean ggthemes naniar \
    && Rscript -e 'devtools::install_github(c("hadley/multidplyr","jeremystan/tidyjson","ropenscilabs/skimr"))' \
    && rm -rf /tmp/downloaded_packages/ /tmp/*.rds \
    && rm -rf /var/lib/apt/lists/*

Upvotes: 3

Views: 2160

Answers (2)

dkreeft
dkreeft

Reputation: 682

I am using the following line to stop building the Docker image upon not finding the package you would like to install:

RUN R -e 'stopifnot("devtools" %in% installed.packages()[,"Package"])'

Make sure to replace devtools with the name of the package you are trying to install.

Upvotes: 0

hrbrmstr
hrbrmstr

Reputation: 78832

save this to something like package_check.R and then have a Docker line that runs it via Rscript:

c("tidytext", "janitor", "corrr", "officer", "devtools", "pacman", "tidyquant", 
  "timetk", "tibbletime", "sweep", "broom", "prophet", "forecast", "prophet", 
  "lime", "sparklyr", "rsparkling", "formattable", "httr", "rvest", "xml2", 
  "jsonlite", "textclean", "ggthemes", "naniar") -> chk_pkgs

suppressPackageStartupMessages(
  sapply(chk_pkgs, require, character.only=TRUE, quietly=FALSE, warn.conflicts=FALSE)
) -> ret

missing_pkgs <- sort(names(which(ret == FALSE)))

if (length(missing_pkgs) > 0) {
  warning("The following packages are not installed: %s", 
          paste0(sprintf("  - %s", missing_pkgs), collapse="\n"))
}

quit(save=FALSE, status=length(names) == 0, runLast = FALSE)

That will give you a missing package error with the missing list and exit the script with a non-zero exit status.

Upvotes: 2

Related Questions