如何在Shiny中展示存储在本地的多个图片(png)

3

我在网上搜索了很多类似的问题但都没有找到与我的情况相符的答案。我对Shiny非常陌生,所以希望你们能帮助我。下面是我编写的代码。我在本地文件夹中有许多png文件的标题格式如下:“CountyX reasonY.png”(文件扩展名前有一个空格),在UI中,你可以看到在下拉菜单中选择多个县和原因进行组合。

我想做的是能够将多个图像呈现在栅格中,以便比较各县和原因的组合(即在2x2网格(如果有12个,则为3x4)中查看County2 reason1、County1 reason3、County2 reason3和County4 reason2等)。为了使它更容易,我已经上传了一些png文件供实验使用:PNG 1PNG 2PNG 3PNG 4

我尝试过的output$plots仅适用于显示一个图像。

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  titlePanel("Compare"),
  sidebarLayout(
    sidebarPanel(
      pickerInput(inputId = "countyInput", label = "Filter county",
                  choices = c("County1", "County2", "County3", "County4", "County5"),
                  options = list(`actions-box` = TRUE,size = 10, `selected-text-format` = "count > 9"),
                  multiple = TRUE),
      checkboxGroupInput(inputId = "reasonInput", label = "Filter reason",
                         choices = c("reason1", "reason2", "reason3"))
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)

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

  output$plot <- renderImage({
    filename <- normalizePath(file.path("<local path name to png files>", paste(input$countyInput, " ", input$reasonInput, " .png", sep = "")))
    list(src = filename)  
    }, deleteFile = FALSE)

}

shinyApp(ui = ui, server = server)

感谢您的所有帮助。
1个回答

3
使用 gridgridExtrapng,您可以将 png 渲染到一个“图形”中。
library(shiny)
library(shinyWidgets)
library(gridExtra)
library(png)
library(grid)

ui <- fluidPage(
  titlePanel("Compare"),
  sidebarLayout(
    sidebarPanel(
      pickerInput(inputId = "countyInput", label = "Filter county",
                  choices = c("County1", "County2", "County3", "County4", "County5"),
                  options = list(`actions-box` = TRUE,size = 10, `selected-text-format` = "count > 9"),
                  multiple = TRUE),
      checkboxGroupInput(inputId = "reasonInput", label = "Filter reason",
                         choices = c("reason1", "reason2", "reason3"))
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)

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

  output$plot <- renderPlot({
    filename <- normalizePath(file.path("<path>", paste0(input$countyInput, " ", input$reasonInput, ".png", sep = ""))) # you had one extra space before .png
    filename <- filename[file.exists(filename)]
    pngs = lapply(filename, readPNG)
    asGrobs = lapply(pngs, rasterGrob)
    p <- grid.arrange(grobs=asGrobs, nrow = 1)
    }, width = 1000)
}

shinyApp(ui = ui, server = server)

当然,你需要一些错误处理来处理不完整的文件名。此外,你可以使用 length(filename) 作为 nrowncol 的条件,以获得所需的网格单元格数量。


1
这真的很有帮助!感谢您教我关于pngs包的知识。我也学到了paste0和paste之间的区别。非常感谢您的帮助!附言:我的png文件名在“.png”前有一个额外的空格是有意为之的。 - kindofhungry
你有没有想过如何调整图片大小?我的图片显示得太小了,很难阅读。另外,如果将图片放大,会使窗口可滚动还是截断它? - kindofhungry
1
我在我的答案中添加了 width。如 https://shiny.rstudio.com/reference/shiny/0.14/renderPlot.html 所述,您可以使用反应性值来指定宽度。因此,也许可以为图像数量创建一个反应表达式,并在指定 nrowwidth 时使用它。 - annhak
1
顺便提一下,width 属性会使窗口可以滚动,而不会裁剪图像。 - annhak
1
我用文件名编辑了我的答案 <- filename [file.exists(filename)] -line - annhak
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接