Description Usage Arguments Value Event types Event properties Note See Also Examples
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
.
This action can be reverted by calling removeEvent
.
1 2 3 |
id |
The id of the element/Shiny tag |
expr |
The R expression or function to run after the event is triggered. If a function with an argument is provided, it will be called with the JavaScript Event properties as its argument. Using a function can be useful when you want to know, for example, what key was pressed on a "keypress" event or the mouse coordinates in a mouse event. See below for a list of properties. |
add |
If |
asis |
If |
event |
The event that needs to be triggered to run the code. See below for a list of event types. |
properties |
A list of JavaScript Event properties that should be available
to the argument of the |
An ID that can be used by removeEvent
to remove
the event listener. See removeEvent
for more details.
Any standard mouse or
keyboard events
that are supported by JQuery can be used. The standard list of events that can be used is:
click
, dblclick
, hover
, mousedown
, mouseenter
,
mouseleave
, mousemove
, mouseout
, mouseover
, mouseup
,
keydown
, keypress
, keyup
. You can also use any other non
standard events that your browser supports or with the use of plugins (for
example, there is a mousewheel
plugin that you can use to listen to mousewheel events).
If a function is provided to expr
, the function will receive a list
of JavaScript Event properties describing the current event as an argument.
Different properties are available for different event types. The full list
of properties that can be returned is: altKey
, button
,
buttons
, clientX
, clientY
, ctrlKey
, pageX
,
pageY
, screenX
, screenY
, shiftKey
, which
,
charCode
, key
, keyCode
, offsetX
, offsetY
.
If you want to retrieve any additional properties that are available in
JavaScript for your event type, you can use the properties
parameter.
shinyjs
must be initialized with a call to useShinyjs()
in the app's ui.
removeEvent
,
useShinyjs
,
runExample
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | if (interactive()) {
library(shiny)
shinyApp(
ui = fluidPage(
useShinyjs(), # Set up shinyjs
p(id = "date", "Click me to see the date"),
p(id = "coords", "Click me to see the mouse coordinates"),
p(id = "disappear", "Move your mouse here to make the text below disappear"),
p(id = "text", "Hello")
),
server = function(input, output) {
onclick("date", alert(date()))
onclick("coords", function(event) { alert(event) })
onevent("mouseenter", "disappear", hide("text"))
onevent("mouseleave", "disappear", show("text"))
}
)
}
## Not run:
# 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")
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.