Learn R Programming

shinyjs (version 0.0.6.0)

onclick: Run R code when an element is clicked

Description

Run an R expression (either a shinyjs function or any other code) when an element is clicked. The most sensible use case is to run code after clicking a button or a link, but this function can be used on any other HTML element as well.

Usage

onclick(id, expr, add = FALSE)

Arguments

id
The id of the element/Shiny tag
expr
The R expression to run after the element gets clicked
add
If TRUE, then add expr to be executed after any other code that was previously set using onclick; otherwise expr will overwrite any previous onclick expressions. This parameter works well in browsers

See Also

useShinyjs, runExample

Examples

Run this code
# Note these examples use several other shinyjs functions as the onclick code
if (interactive()) {
  shiny::shinyApp(
    ui = shiny::fluidPage(
      useShinyjs(),  # Set up shinyjs
      shiny::actionButton("btn", "Click me"),
      shiny::p(id = "element", "Click me to change my text")
    ),
    server = function(input, output, session) {
      # Change the following lines for more examples
      onclick("btn", info(date()))
      onclick("element", text("element", "Hello!"))
    }
  )
}
# The shinyjs function call in the above app can be replaced by
  # any of the following examples to produce similar Shiny apps
  onclick("btn", toggle("element"))
  onclick(expr = text("element", date()), id = "btn")
  {onclick("btn", info(date())); onclick("btn", info("Another message"), TRUE)}

Run the code above in your browser using DataLab