0

我正在尝试使用以下 Shiny 网站提供的示例代码片段将本地图像文件渲染到 Shiny 应用程序界面:

ui <- fluidPage(
    imageOutput("plot3")
  )
  
  server <- function(input, output, session) {
    # Send a pre-rendered image, and don't delete the image after sending it
    # NOTE: For this example to work, it would require files in a subdirectory
    # named images/
    output$plot3 <- renderImage({
      filename <- normalizePath(file.path('./images',
                                          paste('image', '2', '.jpeg', sep='')))
      
      # Return a list containing the filename
      list(src = filename, alt = "Alternate text")
    }, deleteFile = FALSE)
  }
  
  shinyApp(ui, server)

当我运行应用程序时它不起作用。但是,当我添加回interactive()最初在示例代码中的函数时,将主代码体包含在内,我能够毫无问题地显示本地图像文件。

if (interactive()) {
  
  ui <- fluidPage(
    imageOutput("plot3")
  )

  server <- function(input, output, session) {...
    
}

这让我感到困惑,因为 Shiny 的许多教程和演示都表明可以在不将代码体封装在范围内的情况下完成渲染。

有没有人遇到过类似的情况?将本地图像文件渲染到 Shiny 应用程序中?

4

1 回答 1

0

我刚刚通过反复试验发现了代码有什么问题。闪亮代码应用程序保存在与图像文件保存位置不同的位置的目录。因此,解决方案是setwd([www image folder location])将图像文件夹 www 放置或重新定位到它工作的闪亮应用程序所在的位置 - 图像已成功渲染。

于 2021-09-09T04:54:20.357 回答