Keep panel height the same as number of plot panels changes

Hi @vinayprakash808. Hi @hadley. I make some edit on @hadley's script to alternate the plot height as

. Hope the script can help.

library(ggplot2)
library(shiny)

ui <- fluidPage(
  selectInput("cyl", "cyl", choices = c("", 4, 6, 8)),
  uiOutput("plot")
  # plotOutput("Plot", width = "1000px", height = "1000px")
)

server <- function(input, output, session) {
  numFacet <- nlevels(factor(mtcars$cyl))
  
  height <- reactive({
    if(input$cyl == "") {
      "1000px"
    } else {
      paste0(floor(1000 / numFacet), "px")
    }
  })
  
  output$plot <- renderUI(plotOutput("Plot", width = "1000px", height = height()))
  
  f_data <- reactive({
    if (input$cyl == "") {
      mtcars
    } else {
      mtcars[mtcars$cyl == input$cyl, ]
    }
  })
  
  output$Plot <- renderPlot({
    f_data() %>%
      ggplot(aes(mpg, disp)) +
      geom_line() +
      facet_wrap(~ cyl , ncol = 1)
  })
}

shinyApp(ui, server)
1 Like

Perfect and thanks a lot

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