Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a conditional panel in Shiny?

Tags:

r

shiny

shinyjs

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.

like image 995
Joe Avatar asked Jan 04 '23 08:01

Joe


2 Answers

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.

like image 168
greg L Avatar answered Jan 16 '23 05:01

greg L


I have never played much with conditionalPanel, so not sure if it has default settings to hide/show. Following might work to give you the desired output.

library(shiny)
library(shinyjs)

if(interactive()){
  shinyApp(
    ui <- fluidPage(
      shinyjs::useShinyjs(),
      actionButton("eval","Evaluate"),
      numericInput("num_input", "If number is changed, cp must hide", value = 0),
      shinyjs::hidden(
        div(
          id = "cp1",
          conditionalPanel(condition = "input.eval", 
                           textOutput("text1")))
        )
      ),

    server = function(input, output, session){
      output$text1 <- renderText({
        input$num_input
      })
      observeEvent(input$eval,{
        shinyjs::show("cp1")
      })
      observeEvent(input$num_input,{
        shinyjs::hide("cp1")
      })
    }
  )
}

I am hiding the conditionalPanel initially using shinyjs, displaying numeric input entered using renderText, and having two observeEvent to hide\show the panel accordingly.

like image 28
Sagar Avatar answered Jan 16 '23 04:01

Sagar