Context: a shiny app allows user to choose to display or not tables and graphs.
Problem: UI keeps a big white "placeholder" space for plots if they are not displayed (this behaviour is not wanted). But, when it comes to tables, UI doesn't display this empty white space (expected behaviour).
Question: How to prevent Shiny from keeping empty placeholder for plots?
Previous researches: This question has already been asked here but the answer by @the-mad-statter doesn't fits my needs. I would like to understand why Shiny has this behaviour and if possible correct it without changing the UI.
Many thanks for yout help! Please find below a reproductible example and some screenshots.
Reproductible example
library(shiny)
library(tidyverse)
# basic user interface
ui <- fluidPage(
checkboxGroupInput(inputId = "checkboxes", label = NULL ,choices = list("Plot"="plot", "Table"="table")),
plotOutput(outputId = "plotdf1"),
tableOutput(outputId = "tabledf"),
plotOutput(outputId = "plotdf2")
)
# server side calculations
server <- function(input, output, session) {
df = mtcars
output$plotdf1 = renderPlot({
if("plot"%in%input$checkboxes){
ggplot(data = df, aes(x = disp, y = qsec)) +
geom_line()
}
})
output$tabledf = renderTable({
if("table"%in%input$checkboxes){
df[1:5,]
}
})
output$plotdf2 = renderPlot({
if("plot"%in%input$checkboxes){
ggplot(data = df, aes(x = gear)) +
geom_bar()
}
})
}
shinyApp(ui,server)
Screenshots: All objects displayed:
When the table is removed the UI "fill"/removes the space left empty
But when plots are removed, empty placeholder remains


