首页 > 解决方案 > 使用 Dockerfile 安装 R 包

问题描述

我正在尝试使用 Dockerfile 安装 R 包。

我安装了图像Rocker/Rstudio

Dockerfile 的内容是:

# Base image https://hub.docker.com/r/rocker/rstudio
FROM rocker/rstudio:latest

## Create directories
RUN mkdir -p /rstudio
RUN mkdir -p /rscripts


##Install R packages
RUN R -e "install.packages(c('rvest', 'jsonlite', 'data.table' ,'stringr'), repos = 'http://cran.us.r-project.org')"

然后我构建我docker build -t my_r_image . 的图像: 图像构建但未安装软件包,因为我收到此警告:

将软件包安装到“/usr/local/lib/R/site-library”(因为“lib”未指定)警告:无法访问存储库的索引 http://cran.us.r-project.org/src/contrib : 无法打开 URL ' http://cran.us.r-project.org/src/contrib/PACKAGES '

我尝试通过替换http://cran.us.r-project.org= 'http://cran.rstudio.com/')"但我得到一个类似的错误,说它无法访问存储库的索引。

任何人都知道我做错了什么?

编辑1:请参阅下面的输出,当我这样做时:

.libPaths()
    [1] "/usr/local/lib/R/site-library" "/usr/local/lib/R/library"     

install.packages(c('rvest', 'jsonlite', 'data.table' ,'stringr'), repos = 'http://cran.us.r-project.org')
        Warning in install.packages :
          unable to access index for repository https://mran.microsoft.com/snapshot/2019-10-15/src/contrib:
          cannot open URL 'https://mran.microsoft.com/snapshot/2019-10-15/src/contrib/PACKAGES'
        Installing packages into ‘/usr/local/lib/R/site-library’
        (as ‘lib’ is unspecified)
        Warning in install.packages :
          unable to access index for repository http://cran.us.r-project.org/src/contrib:
          cannot open URL 'http://cran.us.r-project.org/src/contrib/PACKAGES'
        Warning in install.packages :
          packages ‘rvest’, ‘jsonlite’, ‘data.table’, ‘stringr’ are not available (for R version 3.6.1)

标签: rdockerpackagedockerfile

解决方案


奇怪的是,使用完全相同的 Dockerfile,我使用 rocker/tidyverse 存储库而不是 rocker/rstudio 安装软件包没有问题。有谁知道这是为什么?


推荐阅读