Shiny layout - scale multiple graphs

I wanted to expand on a question I had about adding up to four plots on a page (code in prior post) Shiny - Generate Plots with actionButton. In this current layout there are spaces for the graphs that haven't been generated. Is there a way to automatically scale the graph space by the number of plots?

library(shiny)
library(ggplot2)
library(tidyverse)
library(cowplot)
# devtools::install_github("thomasp85/patchwork")
# library(patchwork)
ui <- fluidPage(
 
 titlePanel("4 Plot Generator"),
 
 sidebarLayout(
  
  sidebarPanel(
   div("Select an x and y then click Add Graph to plot. You can plot and compare up to 4 graphs.
          To reset the plots click Reset Graphs", style="text-align:center;"),
   fluidRow(
    column(6, selectInput("x_iris", "X:", choices= c(unique(colnames(iris))))),
    column(6, selectInput("y_iris", "Y:", choices = c(unique(colnames(iris)))))
   ),
   
   # button to add graph
   # button to remove graph
   fluidRow(
    column(6, actionButton("add_graph", "Add Graph")),
    column(6, actionButton("reset_graph", "Reset Graphs"))
   )
   
  ),
  
  # Show graph
  mainPanel(
   plotOutput("plots")
  )
 )
)


server <- function(input, output) {
 
 # assign graph to reactive value? 
 current_graph <- reactive({ 
  ggplot(iris, aes_string(x = input$x_iris, y = input$y_iris)) + 
   geom_point() 
 })
 
 vals <- reactiveValues(p = 1, gg1 = NULL, gg2 = NULL, gg3 = NULL, gg4 = NULL)
 
 observeEvent(
  input$add_graph,
  {
   req(vals$p <= 4)
   switch (as.character(vals$p),
    "1" = {vals$gg1 = current_graph()},
    "2" = {vals$gg2 = current_graph()},
    "3" = {vals$gg3 = current_graph()},
    "4" = {vals$gg4 = current_graph()}
   )
   vals$p <- vals$p + 1
  }
 )
 
 observeEvent(
  input$reset_graph,
  {
   vals$p <- 1
   vals$gg1 <- NULL
   vals$gg2 <- NULL
   vals$gg3 <- NULL
   vals$gg4 <- NULL
  }
 )
 
 output$plots <- renderPlot({
  plot_grid(plotlist = list(vals$gg1, vals$gg2, vals$gg3, vals$gg4), ncol = 2)
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.