Hide/show outputs Shiny R

前端 未结 3 1263
庸人自扰
庸人自扰 2020-12-11 05:21

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

3条回答
  •  無奈伤痛
    2020-12-11 05:35

    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)
    

提交回复
热议问题