【发布时间】:2021-11-04 20:55:37
【问题描述】:
我正在尝试使用以下 Shiny 网站提供的示例代码的 sn-p 将本地图像文件渲染到 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 应用中?
【问题讨论】:
-
请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。
-
请尽量放图片大小:
list(src = filename, contentType = 'image/jpeg', width = 400, height = 300, alt = "Alternate text") -
这能回答你的问题吗? Display locally-stored image in R Shiny
-
感谢大家提供的任何帮助。它们是非常好的参考。我刚刚通过反复试验发现了代码有什么问题。闪亮代码应用程序保存在与图像文件保存位置不同的位置的目录。因此,解决方案是放置一个
setwd([www image folder location])或将图像文件夹 www 重新定位到它工作的闪亮应用程序所在的位置 - 图像已成功渲染。谢谢!