闪亮的渲染图并排显示,大小随图数的变化而变化

shiny render plots side by side with size change with number of plots

我想渲染我的绘图,绘图的大小根据显示的绘图数量而变化。

这是我的 UI 代码:

 box(width=NULL, title = "Comparison", collapsible = TRUE, collapsed = TRUE, 
fluidRow(column(7, checkboxGroupInput("checkGroup", label = "Dataset to compare:", choices = list("1", "2", "3"), selected = "1"))),
fluidRow(column(12, uiOutput("plot_list")))
)

这是我的服务器代码:

  output$plot_list <- renderUI({
    req(input$checkGroup)
    output = tagList()
    
    if(any(input$checkGroup %in% "1")){
      output[[1]] <- renderVisNetwork({makeVisnetwork(visOutputs[[1]]$coef_tbl)})
    }
    if(any(input$checkGroup %in% "2")){
      output[[2]] <- renderVisNetwork({makeVisnetwork(visOutputs[[2]]$coef_tbl)})
    }
    if(any(input$checkGroup %in% "3")){
      output[[3]] <- renderVisNetwork({makeVisnetwork(visOutputs[[3]]$coef_tbl)})
    }
    
    output
  })

这使得每个绘图一个接一个(垂直)占据仪表板的整个行宽。我想要这样,如果我只检查“1”,那么整个行宽都显示“1”,但是如果我检查“1”和“2”,那么该行将分成 2 并并排显示图,依此类推“3”。

我尝试将最后 output 行更改为 do.call(flowLayout, output) 但这不会根据绘图数量动态拆分页面。

library(shiny)
library(ggplot2)

ui <- fluidPage(
  br(),
  checkboxGroupInput(
    "cbg", "Plots", choices = list("a", "b", "c"), selected = "a"
  ),
  br(),
  uiOutput("plots")
)

server <- function(input, output, session){
  
  output[["plota"]] <- renderPlot({
    ggplot(
      iris, aes(x = Sepal.Length, y = Sepal.Width)
    ) + geom_point()
  })

  output[["plotb"]] <- renderPlot({
    ggplot(
      iris, aes(x = Sepal.Length, y = Petal.Width)
    ) + geom_point()
  })

  output[["plotc"]] <- renderPlot({
    ggplot(
      iris, aes(x = Petal.Length, y = Sepal.Width)
    ) + geom_point()
  })
  
  output[["plots"]] <- renderUI({
    Plots <- list()
    if("a" %in% input[["cbg"]]){
      Plots <- c(Plots, list(plotOutput("plota")))
    }
    if("b" %in% input[["cbg"]]){
      Plots <- c(Plots, list(plotOutput("plotb")))
    }
    if("c" %in% input[["cbg"]]){
      Plots <- c(Plots, list(plotOutput("plotc")))
    }
    do.call(splitLayout, Plots)
  })
}

shinyApp(ui, server)