Learn R Programming

shinyjs (version 0.3.1)

onevent: Run R code when an event is triggered on an element

Description

onclick runs an R expression (either a shinyjs function or any other code) when an element is clicked. onevent is similar, but can be used when any event is triggered on the element, not only a mouse click. See below for a list of possible event types. Using "click" results in the same behaviour as calling onclick.

Usage

onclick(id, expr, add = FALSE)

onevent(event, id, expr, add = FALSE)

Arguments

id
The id of the element/Shiny tag
expr
The R expression to run after the event is triggered
add
If TRUE, then add expr to be executed after any other code that was previously set using onevent or onclick; otherwise expr will overwrite any previous expressions. Note that this paramete
event
The event that needs to be triggered to run the code. See below for a list of possible event types.

Possible event types

Any http://api.jquery.com/category/events/mouse-events/{mouse} or http://api.jquery.com/category/events/keyboard-events/{keyboard} events that are supported by JQuery can be used. The full list of events that can be used is: click, dblclick, hover, mousedown, mouseenter, mouseleave, mousemove, mouseout, mouseover, mouseup, keydown, keypress, keyup.

See Also

useShinyjs, runExample

Examples

Run this code
if (interactive()) {
  shiny::shinyApp(
    ui = shiny::fluidPage(
      useShinyjs(),  # Set up shinyjs
      shiny::p(id = "date", "Click me to see the date"),
      shiny::p(id = "disappear", "Move your mouse here to make the text below disappear"),
      shiny::p(id = "text", "Hello")
    ),
    server = function(input, output) {
      onclick("date", info(date()))
      onevent("mouseenter", "disappear", hide("text"))
      onevent("mouseleave", "disappear", show("text"))
    }
  )
}
# The shinyjs function call in the above app can be replaced by
  # any of the following examples to produce similar Shiny apps
  onclick("disappear", toggle("text"))
  onclick(expr = text("date", date()), id = "date")

Run the code above in your browser using DataLab