0

I am trying to do something which I thought would be relatively simple, but I cannot seem to figure it out.

I am attempting to have an actionLink which, when pressed, provides additional information for the user. When pressed again it hides the information. I can do this fine, but what I am struggling with is updating the text of the actionLink.

I want it to read "Show additional" when the extra information is hidden, and then "Hide additional" when the information is exposed. I have read the following questions/answers, but cannot quite get it to work.

Modify shiny action button once it is clicked

Update label of actionButton in shiny

I have provided a simple code for this below, though the real example will be a lot more complex.

Thank you for your time and help.

shinyApp(
  ui = shinyUI(fluidPage(useShinyjs(),
                         actionLink("button", "Show additional"),
                         hidden(
                           div(id='text_div',
                               verbatimTextOutput("text")
                               )
                           )
                         )
               ),

  server = function(input, output, session){
    observeEvent(input$button, {
      toggle('text_div')
      output$text <- renderText({"Additional"})
    })

  }
)

1 Answer 1

5

You can check for the value of input$button(which increments by 1 each time you click on it) and update the actionLink label parameter in function of its value with updateActionButton :

shinyApp(
  ui = shinyUI(
    fluidPage(useShinyjs(),
              actionLink("button", "Show additional"),
              hidden(div(id='text_div', verbatimTextOutput("text")))
    )
  ),

  server = function(input, output, session){
    observeEvent(input$button, {
      toggle('text_div')
      output$text <- renderText({"Additional"})

      if (input$button %% 2 == 1) {
        txt <- "Hide Additional"
      } else {
        txt <- "Show Additional"
      }
      updateActionButton(session, "button", label = txt)
    })

  }
)
1
  • Perfect! Thank you! I was almost there, but just was missing the correct formatting of the modulo operator. Appreciate your time and help.
    – FitzKaos
    Feb 22, 2018 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.