3

I'm trying to write a simple shiny app with an actionButton. When the actionButton is pressed, some text output should be printed directly below. The code below takes me part way to a solution:

shinyApp(
  ui = shinyUI( fluidPage(
    actionButton("button", "don't press the button"),
    verbatimTextOutput("text")
  )
  ),
  server = function(input, output, session){
    observeEvent(input$button, {
      output$text <- renderText({"ahh you pressed it"})
    })
  }
)

There are two things I'd like to change, but not sure how:

1) The code above shows an empty gray box before the button is pressed - I'd like there to be nothing there prior to the button being pressed. It looks like conditionalPanel might be the right approach, but not sure how to implement this.

2) Can the above code be adapted so that once the button is pressed a second time, the text output is hidden again?

ally
  • 53
  • 1
  • 3
  • 1
    1) I cannot confirm that - no gray box here. 2) you can use the `shinyjs` package, include `useShinyjs()` in `fluidPage`, set up text as `hidden(verbatimTextOutput("text"))` and add ` toggle("text")` in the ovbserver, so that the hidden/visible status of _text_ is toggled on each click. – lukeA Jun 16 '17 at 10:16

1 Answers1

7

You can try this with shinyjs, hidden and toggle

library(shiny)
library(shinyjs)

shinyApp(
  ui = shinyUI(fluidPage(useShinyjs(), 
    actionButton("button", "don't press the button"),
    hidden(
      div(id='text_div',
        verbatimTextOutput("text")
        )
    )
  )
  ),
  server = function(input, output, session){
    observeEvent(input$button, {
      toggle('text_div')
      output$text <- renderText({"ahh you pressed it"})
    })

  }
)
Antoine N.
  • 151
  • 8