在 Docker 环境中运行 R 包 reticulate

Ale*_*nez 13 r docker dockerfile reticulate

在之前的工作中,我使用“reticulate”包在 R 中运行 Autogluon autoML 库。该代码在我当前的配置(Ubuntu 20.4、R 4.10、reticulate v.125)中运行良好。

然而,这段代码在 Docker 中不起作用。

Dockerfile

FROM rocker/r-ver:4.1.0

## Install R packages
RUN R -q -e 'install.packages("remotes")'
RUN R -q -e 'remotes::install_github("rstudio/reticulate")'

# Install Autogluon
RUN R -q -e 'reticulate::install_miniconda()'
RUN R -q -e 'reticulate::conda_create(envname = "r-autogluon", packages = c("python=3.8.13", "numpy"))'
# RUN R -q -e 'reticulate::conda_list()'
RUN R -q -e 'reticulate::conda_install(envname = "r-autogluon", packages = "autogluon", pip = TRUE)'
RUN R -e 'reticulate::use_condaenv("r-autogluon", required = TRUE)'
# RUN -q -e 'reticulate::py_config()'

EXPOSE 3838
CMD R -e 'reticulate::import("autogluon.tabular")'

# Run in shell
# sudo docker build --no-cache -t demo .
# sudo docker run --rm -p 3838:3838 demo
Run Code Online (Sandbox Code Playgroud)

我遇到这个错误,我不知道如何解决它!

reticulate::import("autogluon.tabular") py_module_import(module, Convert = Convert) 中的错误:ModuleNotFoundError:没有名为“autogluon”的模块调用:-> py_module_import 执行停止

曲目

  • 'conda_list()' 表明“r-autogluon”已成功创建!
  • 'py_config()' 表示默认使用“r-reticulate”。
  • 'reticulate::use_condaenv("r-autogluon", required = TRUE)' 不起作用。

有人有解决办法吗?

Wal*_*ldi 9

RUN R -e执行命令并关闭会话: reticulate::import找不到autogluon.tabular,因为reticulate::use_condaenv('r-autogluon')在另一个会话中运行,因此r-autogluon当前会话中未设置环境。

为了在会话启动时运行特定的初始化命令,您需要修改Rprofile请参阅 R 启动,或?Startup

您可以COPY修改版本Rprofile,或直接修改该文件:

FROM rocker/r-ver:4.1.0

## Install R packages
RUN R -q -e 'install.packages("remotes")'
RUN R -q -e 'remotes::install_github("rstudio/reticulate")'

# Install Autogluon
RUN R -q -e 'reticulate::install_miniconda()'
RUN R -q -e 'reticulate::conda_create(envname = "r-autogluon", packages = c("python=3.8.13", "numpy"))'
# RUN R -q -e 'reticulate::conda_list()'
RUN R -q -e 'reticulate::conda_install(envname = "r-autogluon", packages = "autogluon", pip = TRUE)'


## Modify Rprofile
RUN R -e 'write("reticulate::use_condaenv(\"r-autogluon\", required = TRUE)",file=file.path(R.home(),"etc","Rprofile.site"),append=TRUE)'
RUN R -e 'write("reticulate::import(\"autogluon.tabular\")",file=file.path(R.home(),"etc","Rprofile.site"),append=TRUE)'

EXPOSE 3838
Run Code Online (Sandbox Code Playgroud)

以交互模式运行容器现在显示已autogluon.tabular加载(R 会自动启动,因为r-ver docker 文件以 结尾CMD ["R"]):

docker run --rm -ti demo
Run Code Online (Sandbox Code Playgroud)
R version 4.1.0 (2021-05-18) -- "Camp Pontanezen"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

Module(autogluon.tabular)
>  
 
Run Code Online (Sandbox Code Playgroud)