Home > Software design >  Start shiny with a hidden output that can be shown with toggle
Start shiny with a hidden output that can be shown with toggle

Time:12-13

I've seen how to create an actionButton that can hide and then show an output from shiny. For example, the below code, based on this link:

dat <- data.frame(a=1:10, b=rexp(10, 1/10), c=letters[sample(1:24, 10)])

shinyApp(
  ui = fluidPage(
    useShinyjs(),
    actionButton("hide", "Toggle"),
    p("Text above plot"),
    plotOutput("plot"),
    p("Text below plot")
  ),
  server = function(input, output, session) {
    output$plot <- renderPlot({
      plot(b ~ a, data=dat)
    })

    observeEvent(input$hide, {
      toggle("plot") # if you want to alternate between hiding and showing
    })
  },
  options = list(height = 700)
)

The plot starts shown, and then you can click the Toggle button to make it disappear, and then appear again. However, I need it to begin hidden, and then the user can click to make it appear and then disappear.

How can I make it beggin hidden? Thanks!

CodePudding user response:

You can can toggle('plot') inside the server but outside a reactive context so it would execute once when the app starts. For example:

    toggle('plot')
    
    observeEvent(input$hide, {
      toggle("plot") # if you want to alternate between hiding and showing
    })
  • Related