Search code examples
rshinyshow-hide

Hide/show outputs Shiny R


I am trying to find out how to show and hide my outputs like graphics and tabels each time when the user change something in the widgets. For instance I have a sliderInput for my variable called "gender" with 2 choices : male and female. I also have a button which executes estimations when the user click on it. I want to hide the outputs each time when the user changes at least one choice between the different widgets. For instance after one estimation the user decides to change only the level of education and when the user click on the sliderInput box, I would like to hide the previous results.

I tried to use the R package shinyjs and the functions hide/show but they are not working for outputs.

Do you have any idea how to do it without using shinyjs package?

Here is a part of my code:

shinyUI(fluidPage(

  sidebarLayout(
    fluidRow( 
      column(4, wellPanel(

  fluidRow(
      column(5,selectInput("gender",
                          label = div("Sexe",style = "color:royalblue"),
                          choices = list("Male", "Female"),
                          selected = "Female")),

        # other different widjets..        

              column(8, plotOutput('simulationChange')),
              column(4, tableOutput('simulationChangeTable'),
                                    tags$style("#simulationChangeTable table {font-size:9pt;background-color: #E5E4E2;font-weight:bold;margin-top: 121px; margin-left:-30px;overflow:hidden; white-space:nowrap;text-align:left;align:left;}", 
                                    media="screen", 
                                    type="text/css"), 
                  fluidRow(
                     column(6, tableOutput('simulationChangeEsperance'),
                                    tags$style("#simulationChangeEsperance table {font-size:9pt;background-color: #E5E4E2;font-weight:bold;margin-top: -10px; margin-left:-30px;overflow:hidden; white-space:wrap;word-break: break-word;width:173px;text-align:left;}"))
                  )
              )
            )
        )
     )
    ))  

shinyServer(function(input, output, session) {
# part of my server.R code
    observe({


   if (input$gender|input$age|input$birthplace|input$education){  
      shinyjs::hide("simulationChange")
      shinyjs::hide("simulationChangeTable")
      shinyjs::hide("simulationChangeEsperance")
      }      
})

Thank you.


Solution

  • As mentioned by Dieter in the comments you need to use conditionalPanel. For example, in your ui.R, instead of

    plotOutput('simulationChange')
    

    use

    conditionalPanel("output.show", plotOutput('simulationChange'))
    

    And in your server.R add the following:

    values <- reactiveValues()
    values$show <- TRUE
    
    observe({
        input$gender
        input$age
        input$birthplace
        input$education
        values$show <- FALSE
    })
    
    output$show <- reactive({
        return(values$show)
    })
    

    Also, don't forget to change values$show, when clicking on your button:

    observeEvent(input$button, {
        ...
        values$show <- TRUE
    })