使用docker文件安装R软件包

问题描述 投票:18回答:4

我已经在docker文件中使用以下行安装了R。请建议我现在如何指定要在我的docker文件中安装的软件包。

RUN yum -y install R-core R-devel

我正在做这样的事情:

RUN R -e "install.packages('methods',dependencies=TRUE, repos='http://cran.rstudio.com/')"\
    && R -e "install.packages('jsonlite',dependencies=TRUE, repos='http://cran.rstudio.com/')" \
    && R -e "install.packages('tseries',dependencies=TRUE, repos='http://cran.rstudio.com/')" 

这是正确的方法吗?

r docker dockerfile yum install.packages
4个回答
13
投票

如@Cameron Kerr的评论所建议,Rscript不会给您构建失败。到目前为止,推荐的方法是按照问题的建议进行操作。

RUN R -e "install.packages('methods',dependencies=TRUE, repos='http://cran.rstudio.com/')"
RUN R -e "install.packages('jsonlite',dependencies=TRUE, repos='http://cran.rstudio.com/')"
RUN R -e "install.packages('tseries',dependencies=TRUE, repos='http://cran.rstudio.com/')" 

如果您确定没有包装失败,请使用这种单线-

RUN R -e "install.packages(c('methods', 'jsonlite', 'tseries'),
                           dependencies=TRUE, 
                           repos='http://cran.rstudio.com/')"

EDIT:如果您不使用Base-R图像,则可以使用rocker-orgr-verr-studiotidyverse图像。这是repo。这是一个示例Dockerfile-

FROM rocker/tidyverse:latest

# Install R packages
RUN install2.r --error \
    methods \
    jsonlite \
    tseries

--error标志是可选的,如果软件包安装失败(这将导致install.packages()命令失败),它将使docker build引发错误。默认情况下,install.packages()仅引发警告,这意味着即使Dockerfile安装失败也可以成功构建。

所有rocker-org基本上是install littler功能的install2.R


8
投票

是的,您的解决方案应该有效。我遇到了同样的问题,并在https://github.com/glamp/r-docker/blob/master/Dockerfile中找到了解决方案。

简而言之,使用:RUN Rscript -e "install.packages('PACKAGENAME')"。我已经尝试过了,并且有效。


1
投票

[您可以使用所需的安装命令编写R脚本,然后使用Docker运行它-如果我正确阅读了本文档(https://hub.docker.com/_/r-base/)。

FROM r-base
COPY . /usr/local/src/myscripts
WORKDIR /usr/local/src/myscripts
CMD ["Rscript", "myscript.R"]

Build your image with the command:

$ docker build -t myscript /path/to/Dockerfile

myscript.R包含适当的软件包安装命令。


0
投票

R -e "install.packages..."方法在软件包安装失败时并不总是产生错误。

我基于Cameron Kerr的答案here编写了一个脚本,如果无法加载该软件包,则会产生错误,并中断Docker构建过程。它可以从R软件包库,GitHub或给定完整URL的源安装软件包。它还会打印安装时间,以帮助计划在一个命令中将哪些软件包组合在一起。

Dockerfile中的示例用法:

# Install from CRAN repo:
RUN Rscript install_packages_or_die.R https://cran.rstudio.com/ Cairo
RUN Rscript install_packages_or_die.R Cairo # Uses default CRAN repo
RUN Rscript install_packages_or_die.R jpeg png tiff # Multiple packages

# Install from GitHub:
RUN Rscript install_packages_or_die.R github ramnathv/htmlwidgets
RUN Rscript install_packages_or_die.R github timelyportfolio/htmlwidgets_spin spin

# Install from source given full URL of package:
RUN Rscript install_packages_or_die.R https://cran.r-project.org/src/contrib/Archive/curl/curl_4.0.tar.gz curl

这里是脚本:

#!/usr/bin/env Rscript

# Install R packages or fail with error.
#
# Arguments:
#   - First argument (optional) can be one of:
#       1. repo URL
#       2. "github" if installing from GitHub repo (requires that package 'devtools' is
#          already installed)
#       3. full URL of package from which to install from source; if used, provide package
#          name in second argument (e.g. 'curl')
#     If this argument is omitted, the default repo https://cran.rstudio.com/ is used.
#   - Remaining arguments are either:
#       1. one or more R package names, or
#       2. if installing from GitHub, the path containing username and repo name, e.g.
#          'timelyportfolio/htmlwidgets_spin', optionally followed by the package name (if
#          it differs from the GitHub repo name, e.g. 'spin').

arg_list = commandArgs(trailingOnly=TRUE)

if (length(arg_list) < 1) {
  print("ERROR: Too few arguments.")
  quit(status=1, save='no')
}

if (arg_list[1] == 'github' || grepl("^https?://", arg_list[1], perl=TRUE)) {
  if (length(arg_list) == 1) {
    print("ERROR: No package name provided.")
    quit(status=1, save='no')
  }
  repo = arg_list[1]
  packages = arg_list[-1]
} else {
  repo = 'https://cran.rstudio.com/'
  packages = arg_list
}

for(i in seq_along(packages)){
    p = packages[i]

    start_time <- Sys.time()
    if (grepl("^https?://[A-Za-z0-9.-]+/.+", repo, perl=TRUE)) {
      # If 'repo' is URL with path after domain name, treat it as full path to a package
      # to be installed from source.
      install.packages(repo, repo=NULL, type="source");
    } else if (repo == "github") {
      # Install from GitHub.
      github_path = p
      elems = strsplit(github_path, '/')
      if (lengths(elems) != 2) {
        print("ERROR: Invalid GitHub path.")
        quit(status=1, save='no')
      }
      username = elems[[1]][1]
      github_repo_name = elems[[1]][2]
      if (!is.na(packages[i+1])) {
        # Optional additional argument was given specifying the R package name.
        p = packages[i+1]
      } else {
        # Assume R package name is the same as GitHub repo name.
        p = github_repo_name
      }

      library(devtools)
      install_github(github_path)
    } else {
      # Install from R package repository.
      install.packages(p, dependencies=TRUE, repos=repo);
    }
    end_time <- Sys.time()

    if ( ! library(p, character.only=TRUE, logical.return=TRUE) ) {
      quit(status=1, save='no')
    } else {
      cat(paste0("Time to install ", p, ":\n"))
      print(end_time - start_time)
    }

    if (repo == "github") {
      break
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.