我正在尝试使用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的教程和演示都显示渲染可以在不将代码体包裹在作用域内的情况下完成。
有谁遇到过类似的情况吗?渲染本地图像文件到一个闪亮的应用程序?
我只是通过反复试验发现了代码的错误。保存闪亮代码应用程序的目录与保存图像文件的位置不同。因此,解决方案是放置setwd([www image folder location])
或重新定位图像文件夹www到闪亮的应用程序驻留的地方,它工作-图像成功渲染。