将本地存储的图片对齐到网格中

时间:2019-03-20 01:57:04

标签: r shiny

我在将图片分成UI的单独列时遇到麻烦。我认为这是因为我正在加载包含图像列表的变量,所以无法将图像分成不同的列和行。这是我的代码,我还附了一些图片供您下载并尝试:https://drive.google.com/open?id=14w2WF9pyEsxL9fJ1jFOhw7PzmUV_g4fS

library(shiny)
library(shinyWidgets)
library(gridExtra)
library(ggplot2)

ui <- fluidPage(
  titlePanel("Compare"),
  sidebarLayout(
    sidebarPanel(
      pickerInput(inputId = "countyInput", label = "Select which counties to filter by",
                  choices = c("County1", "County2", "County3", "County4", "County5"),
                  options = list(`actions-box` = TRUE,size = 10, `selected-text-format` = "count > 9"),
                  multiple = TRUE),
      checkboxGroupInput(inputId = "causeInput", label = "Select which causes to filter by",
                         choices = c("reason1", "reason2", "reason3", "reason4"))
    ),
    mainPanel(
      uiOutput("images")
    )
  )
)



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

  file_name <- reactive({
    req(input$countyInput)
    req(input$causeInput)
    paste0(unlist(lapply(input$countyInput, function(i) {
      paste0(i, input$causeInput)
    })), ".png")
  })

  output$images <- renderUI({
    tagList(
      lapply(file_name(), function(file) {
        filename <- file.path("PATH TO PICTURES", file)
        if (file.exists(filename)) {
          output[[paste("img", file, sep = "")]] <- renderImage({
            list(src = filename,
                 alt = paste("Image name", file),
                 width = "806",
                 height = "403")
          }, deleteFile = FALSE)}
      })
    )
  })
}

shinyApp(ui, server)

当前,所有内容都被加载到一列中,但是我希望它根据加载的图片数来具有多列,但是最多2行(每列最多包含2张图片),直到最大列为到达的话,它可以排3行。谢谢您的帮助。

0 个答案:

没有答案