验证安装在docker容器中的R软件包

时间:2017-10-24 04:27:32

标签: r docker dockerfile

有没有人有任何最佳实践来验证R软件包是否已安装到docker容器中?我想设置我的容器来运行CI服务,并验证我的软件包是否已安装,但是因为我一直在本地构建它,所以日志似乎很难确定安装了什么软件包,什么没有。能够让CI服务为我做这件事,或者使用简单的批处理脚本来验证软件包的安装情况会很好。

以下是我当前的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/*

2 个答案:

答案 0 :(得分:1)

将其保存为package_check.R之类的内容,然后设置一个Docker行,通过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)

这将使缺少列表的程序包出现错误,并以非零退出状态退出脚本。

答案 1 :(得分:0)

我使用以下行停止在找不到您要安装的软件包时构建Docker镜像:

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

确保将devtools替换为您要安装的软件包的名称。