我对RShiny、Golem和Docker比较陌生。然而,我已经用Golem框架构建了一个小的分析面板,我想使用Docker在本地部署它。
我已经成功地构建了一个映像,但似乎无法使容器正常运行。当我这样做时,容器初始化,然后停止,并出现以下错误:
> options('shiny.port'=80,shiny.host='0.0.0.0');ShinyPlatform::run_app()
Error in loadNamespace(x) : there is no package called ‘ShinyPlatform’
Calls: loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart
Execution halted
如果有任何帮助的话,我用来运行的命令如下:
docker run shinytest2
我还在Rshine应用程序的项目文件夹中工作(不知道这是否有帮助(。
细节是它使用renv和golem框架,以及CRAN和本地包的混合。下面是我目前使用的dockerfile,其中大部分是使用golem::add_dockerfile()
自动生成的。我修改过的只有以下几行:
RUN R -e 'install.packages("renv/local/LocalPackage1_0.1.0.tar.gz", repos= NULL, type = "source")'
RUN R -e 'install.packages("renv/local/ShinyPlatform_0.0.0.9000.tar.gz", repos = NULL, type = "source")'
上面的第一个是解决我遇到的一个错误,因为LocalPackage1被放在另一个文件夹中而找不到。第二行更多的是我尝试的一次尝试,看看我是否可以做同样的事情来解决当前的错误,到目前为止还没有这样的运气,所以我很确定这是错误的。
这是整个docker文件:
FROM rocker/r-ver:4.1.1
RUN apt-get update && apt-get install -y git-core libcairo2-dev libcurl4-openssl-dev libgit2-dev libicu-dev libssl-dev libxml2-dev make pandoc pandoc-citeproc zlib1g-dev && rm -rf /var/lib/apt/lists/*
RUN echo "options(repos = c(CRAN = 'https://cran.rstudio.com/'), download.file.method = 'libcurl', Ncpus = 4)" >> /usr/local/lib/R/etc/Rprofile.site
RUN R -e 'install.packages("remotes")'
RUN Rscript -e 'remotes::install_version("magrittr",upgrade="never", version = "2.0.1")'
RUN Rscript -e 'remotes::install_version("glue",upgrade="never", version = "1.4.2")'
RUN Rscript -e 'remotes::install_version("processx",upgrade="never", version = "3.5.2")'
RUN Rscript -e 'remotes::install_version("testthat",upgrade="never", version = "3.1.0")'
RUN Rscript -e 'remotes::install_version("htmltools",upgrade="never", version = "0.5.2")'
RUN Rscript -e 'remotes::install_version("attempt",upgrade="never", version = "0.3.1")'
RUN Rscript -e 'remotes::install_version("shiny",upgrade="never", version = "1.7.1")'
RUN Rscript -e 'remotes::install_version("config",upgrade="never", version = "0.3.1")'
RUN Rscript -e 'remotes::install_version("spelling",upgrade="never", version = "2.2")'
RUN Rscript -e 'remotes::install_version("thinkr",upgrade="never", version = "0.15")'
RUN Rscript -e 'remotes::install_version("shinyWidgets",upgrade="never", version = "0.6.2")'
RUN Rscript -e 'remotes::install_version("shinydashboard",upgrade="never", version = "0.7.2")'
RUN Rscript -e 'remotes::install_version("renv",upgrade="never", version = "0.14.0")'
RUN Rscript -e 'remotes::install_version("readxl",upgrade="never", version = "1.3.1")'
RUN Rscript -e 'remotes::install_version("gt",upgrade="never", version = "0.3.1")'
RUN Rscript -e 'remotes::install_version("golem",upgrade="never", version = "0.3.1")'
RUN R -e 'install.packages("renv/local/LocalPackage1_0.1.0.tar.gz", repos= NULL, type = "source")'
RUN R -e 'install.packages("renv/local/ShinyPlatform_0.0.0.9000.tar.gz", repos = NULL, type = "source")'
RUN Rscript -e 'remotes::install_version("DT",upgrade="never", version = "0.19")'
RUN Rscript -e 'remotes::install_version("DBI",upgrade="never", version = "1.1.1")'
RUN Rscript -e 'remotes::install_version("data.table",upgrade="never", version = "1.14.0")'
RUN echo "options(repos = c(CRAN = 'https://cran.rstudio.com/'), download.file.method = 'libcurl', Ncpus = 4)" >> /usr/local/lib/R/etc/Rprofile.site
RUN mkdir /build_zone
ADD . /build_zone
WORKDIR /build_zone
RUN rm -rf /build_zone
EXPOSE 80
CMD R -e "options('shiny.port'=80,shiny.host='0.0.0.0');ShinyPlatform::run_app()"
想知道是否有人知道为什么我的容器停了下来,似乎找不到里面闪亮的应用程序?
如前所述,我对RShiny、Golem和Docker都很陌生,所以如果我遗漏了任何关键信息,请告诉我,我可以更新这篇文章。
以下是我对发生的事情的猜测:
在应用程序的工作目录中启动的R进程将使用项目本地库(由renv
提供(;
在其他工作目录中启动的R进程将使用默认的用户/站点库(取决于R的配置方式(
在Dockerfile中,首先安装所有软件包,然后将WORKDIR
设置到应用程序的目录中。这可能意味着这些包都安装在用户/站点库中,而这些库通常对renv
项目不可见。
一般来说,如果您使用的是renv
,那么拥有以下内容就足够了:
WORKDIR /path/to/project
RUN R -e 'renv::restore()'
RUN R -e <code>
其中<code>
是运行应用程序所需的R代码。(酌情在Dockerfile中包含ADD
和COPY
语句。(
文档位于https://rstudio.github.io/renv/articles/docker.html可能也会有所帮助。
您的软件包是否成功安装在Docker镜像中?RUN R -e 'install.packages("renv/local/ShinyPlatform_0.0.0.9000.tar.gz", repos = NULL, type = "source")
的日志中的输出是什么?
代码中的这个错误通常意味着包含应用程序的程序包没有正确安装——问题是install.packages在失败时不会抛出正确的退出代码,因此即使打包的应用程序未能安装,Docker映像仍会编译。
RUN R -e 'install.packages("renv/local/LocalPackage1_0.1.0.tar.gz", repos= NULL, type = "source")'
RUN R -e 'install.packages("renv/local/ShinyPlatform_0.0.0.9000.tar.gz", repos = NULL, type = "source")'
成功吗?
这些文件在哪里复制到Docker构建?你应该在Dockerfile的某个地方放一个看起来像的东西
ADD renv ./renv
使这些tar.gz可用于构建上下文。
Colin