How to hide a conditional panel in Shiny?

Joe picture Joe · Oct 13, 2017 · Viewed 9.1k times · Source

How to hide a conditional panel in Shiny? Please, see the following example:

library(shiny)

ui <- fluidPage(
  actionButton("eval","Evaluate"),
  numericInput("num_input", "If number is changed, cp must hide", value = 0),
  conditionalPanel(
      condition = "input.eval",
      "text"))

server <- function(input, output, session) {
  observeEvent(input$num_input, {
    input$eval <- 0
  })}

shinyApp(ui, server)

What I want to achieve is: Once the user clicks the evaluate-button the conditional panel should appear, but once the number in num_input is changed the panel should disappear. My idea was to null the evaluate-button, but this does not work (the app opens with gray background and seems frozen).

I also tried it with shinyjs like so:

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  actionButton("eval","Evaluate"),
  numericInput("num_input", "If number is changed, cp must hide", value = 0),
  conditionalPanel(
      id = "cond_panel",
      condition = "input.eval",
      "text"))

server <- function(input, output, session) {
  observeEvent(input$num_input, {
    reset("cond_panel")})}

shinyApp(ui, server)

But this does not work either: the app opens regularly and the conditional panel is shown once the evaluate-button is clicked, but nothing happens once the number is changed.

Answer

greg L picture greg L · Oct 14, 2017

You can create an output value and use it just for the conditional panel. The article on dynamic UI explains how to do this:

http://shiny.rstudio.com/articles/dynamic-ui.html

The condition can also use output values; they work in the same way (output.foo gives you the value of the output foo). If you have a situation where you wish you could use an R expression as your condition argument, you can create a reactive expression in the server function and assign it to a new output, then refer to that output in your condition expression.

If you do this, make sure to also set outputOptions(output, [newOutputName], suspendWhenHidden = FALSE). (This is necessary because Shiny normally doesn’t send values to the browser for outputs that are hidden or not present in the UI. In this case, however, the browser does need to know the most up-to-date output value in order to correctly evaluate the condition of the contitionalPanel function - suspendWhenHidden = FALSE ensures this will happen.)

library(shiny)

ui <- fluidPage(
  actionButton("eval","Evaluate"),
  numericInput("num_input", "If number is changed, cp must hide", value = 0),
  conditionalPanel("input.eval && !output.hide_panel", "text")
)

server <- function(input, output, session) {

  output$hide_panel <- eventReactive(input$num_input, TRUE, ignoreInit = TRUE)

  outputOptions(output, "hide_panel", suspendWhenHidden = FALSE)
}

shinyApp(ui, server)

Another way would be to renderUI the conditional panel, and show it until input$num_input changes.