Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/*
like image 504
petergensler Avatar asked Jan 01 '26 10:01

petergensler


1 Answers

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.

like image 126
hrbrmstr Avatar answered Jan 03 '26 04:01

hrbrmstr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!