2
votes

I'm programming a shiny app with a checkbox group as input and as output there will be the same number of plots like choices.

  • Ticking choice 1 results in displaying plot 1
  • Ticking choice 2 results in displaying plot 2
  • Ticking choice 3 results in displaying plot 3

But when I tick only choice 2 and 3 there will be left some space in the output area where plot 1 would be displayed when it is ticked choice 1. And this white area I don't want to have. The remaining plots should be lifted up.

How can I prevent that there is white space in the output area? I think I have to do some coding in the ui mainPanel area - but I haven't found anything. So I hope you can help me with my issue. Many thanks in advance!

My code example of app.R:

fluidPage(
    
  sidebarLayout(

    sidebarPanel(

       # Group of checkboxes
       checkboxGroupInput("checkGroup", label = h3("Checkbox group"), 
         choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3),
         selected = 1)

    ),

    mainPanel(

      # Must I do here my codeing to prevent the (white) placeholder for the not shown plot?
      plotOutput("plot_Choice1"),
      plotOutput("plot_Choice2"),
      plotOutput("plot_Choice3")

   )
 ) 
)

server <- function(input, output) {

    # Rendering the first plot
    output$plot_Choice1=renderPlot({
        req(input$checkGroup)
        if (any(input$checkGroup == 1)) {
            plot(1:10,main="Plot for choice 1")
        }
    })

    # Rendering the second plot
    output$plot_Choice2=renderPlot({
        req(input$checkGroup)
        if (any(input$checkGroup == 2)) {
        plot(10:20,main="Plot for choice 2")
        }
    })

    # Rendering the third plot
    output$plot_Choice3=renderPlot({
        req(input$checkGroup)
        if (any(input$checkGroup == 3)) {
        plot(20:30,main="Plot for choice 3")
        }
    })



}

# Create Shiny app ----
shinyApp(ui = ui, server = server)
1

1 Answers

2
votes

You can use conditionalPanel to 'hide' the output (note that the condition is JS not R code):

ui <- fluidPage(
  
  sidebarLayout(
    
    sidebarPanel(
      
      # Group of checkboxes
      checkboxGroupInput("checkGroup", label = h3("Checkbox group"), 
                         choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3),
                         selected = 1)
      
    ),
    
    mainPanel(
      conditionalPanel("input.checkGroup.includes('1')", plotOutput("plot_Choice1")),
      conditionalPanel("input.checkGroup.includes('2')", plotOutput("plot_Choice2")),
      conditionalPanel("input.checkGroup.includes('3')", plotOutput("plot_Choice3"))
    )
  ) 
)

server <- function(input, output) {
  
  # Rendering the first plot
  output$plot_Choice1=renderPlot({
    req(input$checkGroup)
    if (any(input$checkGroup == "1")) {
      plot(1:10, main = "Plot for choice 1")
    }
  })
  
  # Rendering the second plot
  output$plot_Choice2=renderPlot({
    req(input$checkGroup)
    if (any(input$checkGroup == "2")) {
      plot(10:20, main = "Plot for choice 2")
    }
  })
  
  # Rendering the third plot
  output$plot_Choice3=renderPlot({
    req(input$checkGroup)
    if (any(input$checkGroup == "3")) {
      plot(20:30, main = "Plot for choice 3")
    }
  })
  
  
  
}

# Create Shiny app ----
shinyApp(ui = ui, server = server)