Search code examples
javascriptshinyaction-button

Moment of click on action button


I have a question concerning the actionButon() function in shiny application. In the RStudio Shiny Web site, it is explained that the actionButton includes some JavaScript code that sends numbers to the server. When the web browser first connects, it sends a value of 0, and on each click, it sends an incremented value: 1, 2, 3, and so on. How is possible to know when the users clicks on the button without using the value of the button?

Thanks


Solution

  • shinyjs package has this functionality via onclick and onevent built in so you can tailor yo your needs. Example below is taken from https://github.com/daattali/shinyjs/issues/33

    if (interactive()) {
      library(shiny)
    
      shinyApp(
        ui = fluidPage(
          useShinyjs(),  # Set up shinyjs
          actionButton("date", "date"),
          actionButton("coords", "coords"),
          actionButton("disappear", "disappear"),
          actionButton("text", "text")
        ),
        server = function(input, output) {
          onclick("date", alert(date()))
          onclick("coords", function(event) { alert(event) })
          onevent("mouseenter", "disappear", hide("text"))
          onevent("mouseleave", "disappear", show("text"))
        }
      )
    }