如何读取 Docker 容器内 R Shiny 应用程序 www 文件夹中的徽标和图像文件?

问题描述 投票:0回答:1

我创建了一个 R闪亮应用程序,其中有一些徽标和图像(favicon.ico、公司徽标等)存储在应用程序的 www 文件夹中。 我为所有文件添加了 COPY 语句,并检查了正在运行的容器,文件就在那里。但闪亮的应用程序本身无法读取文件并显示徽标。 当我在本地运行时,图像显示正确。

这是我的 Dockerfile。

# Base R Shiny image
FROM rocker/shiny

# Make a directory in the container
RUN mkdir /home/shiny-app
RUN mkdir /home/shiny-app/www
RUN mkdir /home/shiny-app/files

WORKDIR /home/shiny-app

# Install R dependencies
#################

# Copy the Shiny app code
COPY app.R /home/shiny-app/app.R
COPY files/* /home/shiny-app/files/
COPY www/* /home/shiny-app/www/

# Expose the application port
EXPOSE 8180

# Run the R Shiny app
CMD Rscript /home/shiny-app/app.R

我正在建设:

docker built -t shiny-analysis-app .
我正在跑步:
docker run --rm -p 8180:8180 shiny-analysis-app

r docker shiny dockerfile shinydashboard
1个回答
0
投票

假设你的

app.R
看起来像这样:

library(shiny)

ui <- fluidPage(
    titlePanel("Old Faithful Geyser Data"),

    sidebarLayout(
        sidebarPanel(
            sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30)
        ),
        mainPanel(
           plotOutput("distPlot"),
           img(src='cat.png', align = "right", width="50%"),
        )
    )
)

server <- function(input, output) {
    output$distPlot <- renderPlot({
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        hist(x, breaks = bins, col = 'darkgray', border = 'white',
             xlab = 'Waiting time to next eruption (in mins)',
             main = 'Histogram of waiting times')
    })
}

shinyApp(ui = ui, server = server)

它包含来自

cat.png
目录的图像
www/
。在主机上测试工作正常。

现在是你的

Dockerfile

FROM rocker/shiny

RUN rm -rf /srv/shiny-server/*

WORKDIR /srv/shiny-server/

COPY app.R ./
COPY files ./files
COPY www ./www

无需显式运行脚本。只需将其放入服务器目录即可。

现在构建并运行。

docker build -t app .
docker run --rm -it -p 3838:3838 app

然后在浏览器中打开http://127.0.0.1:3838/

© www.soinside.com 2019 - 2024. All rights reserved.