Dockerfile: 安装 R 包的方法(包含安装 Github 和 Bioconductor 包) + 安装失败后报错退出 / 判断是否成功安装 R 包

2 篇文章 0 订阅

前提

若使用传统方法

RUN R -e "install.packages('包')"

来安装R包,安装失败后不会有任何提示且继续进行下一步,执行镜像后出现报错不太友好。


方法

安装后不退出(无法判断是否安装成功)

普通包

  1. 添加指定镜像
RUN R -e "install.packages('包'), repos='http://cran.rstudio.com/'"   #官方源
RUN R -e "install.packages('包'), repos='https://mirrors.tuna.tsinghua.edu.cn/CRAN/'"   #清华源

若要安装多个R包install.packages(c('包','包'))

  1. 使用Rscript安装
RUN Rscript -e "install.packages('包')"
  1. 使用install2.r安装
    使用该方法要在最开始指定FROM
    rocker-orgr-verr-studiotidyverseshinyshiny-verse镜像都行
FROM rocker/tidyverse:latest
RUN install2.r 包

若要安装多个R包
① 包 包 #中间以空格作为分隔符

\\

Github包

  1. 使用devtools安装
RUN R -e "install.packages('devtools')"
 && R -e "devtools::install_github('项目来源/项目名')"
  1. 使用installGithub.r安装
RUN installGithub.r 项目来源/项目名 \
 && rm -rf /tmp/downloaded_packages/
# 一个好的做法是删除所有下载的软件包,以减少镜像大小。

BioConductor包

  1. 使用BiocManager安装
RUN R -e "install.packages('BiocManager')"
 && R -e "BiocManager::install('包')"
  1. 使用install.r安装
RUN install2.r -r http://bioconductor.org/packages/3.0/bioc --deps TRUE \\
    && rm -rf /tmp/downloaded_packages/
# 一个好的做法是删除所有下载的软件包,以减少镜像大小。

安装后退出(判断是否安装成功)

普通包

  1. 普通安装
RUN R -e "install.packages('包');     if (!library(包, logical.return=T)) quit(status=10)"
  1. install2.r安装
FROM rocker/tidyverse:latest
RUN install2.r --error 包

Github包

  1. 使用devtools安装
RUN R -e "install.packages('devtools')"
 && R -e "devtools::install_github('项目来源/项目名')"
RUN R -e "if (!library(项目名, logical.return=T)) quit(status=10)"

PS:installGithub.r没有--error参数

BioConductor包

  1. 使用BiocManager安装
RUN R -e "install.packages('BiocManager')"
 && R -e "BiocManager::install('包')"
 RUN R -e "if (!library(包, logical.return=T)) quit(status=10)"
  1. 使用install.r安装
RUN install2.r --error -r http://bioconductor.org/packages/3.0/bioc --deps TRUE \\
    && rm -rf /tmp/downloaded_packages/

参考

  1. adding R package for installation from Github in Dockerfile
  2. how to install from bioconductor using install2.r from littler?(这里有下载Bioconductor包的另一种方法)
  3. Rocker Project: Extending images
  4. Make Docker build stop after if RUN fails in multi-stage build
  5. Install R packages using docker file

我的Dockerfile示例

前略,有指定FROM

# Install library
RUN install2.r --error --skipinstalled \
    shinydashboard DT pheatmap shinyjs shinyWidgets IDPmisc devtools htmlwidgets log4r gplots RcppArmadillo BiocManager reshape2 checkmate lme4 ggrepel rhandsontable viridis

RUN installGithub.r Vitek-Lab/MSstats \
    && rm -rf /tmp/downloaded_packages/
RUN R -e "if (!library(MSstats, logical.return=T)) devtools::install_github('Vitek-Lab/MSstats')"
RUN R -e "if (!library(MSstats, logical.return=T)) quit(status=10)"

RUN R -e "BiocManager::install('AnnotationDbi')" \
 && R -e "BiocManager::install('org.Hs.eg.db')" \
 && R -e "BiocManager::install('org.Sc.sgd.db')" \
 && R -e "BiocManager::install('org.Rn.eg.db')"
RUN install2.r --error --skipinstalled -r http://bioconductor.org/packages/3.0/bioc --deps TRUE \
    AnnotationDbi \
    org.Hs.eg.db \
    org.Sc.sgd.db \
    org.Rn.eg.db \
    && rm -rf /tmp/downloaded_packages/

完整Dockerfile内容见这里


我的另一篇相关文章

在Windows系统下,基于WLS从零开始部署我的 R shiny项目到 Docker 中,并将镜像发布到 Docker Hub(内附详细操作流程)

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值