简体   繁体   中英

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 widjets. For instance I have a slider input 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 widjets. 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.

The reason your code didn't work is because you didn't make a call to useShinyjs() (if you read the documentation or look at any examples of using shinyjs, you'll see that you HAVE to call useShinyjs() in the UI).

I couldn't replicate your code because it had too many errors, but just to demonstrate that it does work with outputs, here's a small example you can run. In your project, just add shinyjs::useShinyjs() somewhere in the UI.

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  actionButton("hideshow", "Hide/show plot"),
  plotOutput("plot")
)

server <- function(input, output, session) {
  output$plot <- renderPlot({
    plot(rnorm(100))
  })

  observeEvent(input$hideshow, {
    # every time the button is pressed, alternate between hiding and showing the plot
    toggle("plot")
  })
}

shinyApp(ui = ui, server = server)

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
})

The other answers here don't seem to provide the right/complete answer. The solution is actually quite simple.

You need to use outputOptions(output, 'show', suspendWhenHidden = FALSE)

Below is a sample code that displays the text inside a conditionalPanel if the dropdown selection is 2 and hides if it is 1.

  library(shiny)
  
  ui <- fluidPage(
    selectInput("num", "Choose a number", 1:2),
    
    conditionalPanel(
      condition = "output.show",
      "The selected number is 2 so this text is displayed. Change it back to 1 to hide."
    )
    
  )
  
  server <- function(input, output, session) {
    output$show <- reactive({
          input$num == 2 # Add whatever condition you want here. Must return TRUE or FALSE
      })
    
    outputOptions(output, 'show', suspendWhenHidden = FALSE)
  }
    
  shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM