|
| 1 | +--- |
| 2 | +title: "JavaScript Events in Shiny" |
| 3 | +author: "Yihui Xie" |
| 4 | +date: "`r Sys.Date()`" |
| 5 | +vignette: > |
| 6 | + %\VignetteEngine{knitr::rmarkdown} |
| 7 | + %\VignetteIndexEntry{JavaScript Events in Shiny} |
| 8 | +output: knitr:::html_vignette |
| 9 | +--- |
| 10 | + |
| 11 | +A number of JavaScript events have been supported in Shiny since v0.13. These events can be used to keep track of the app progress, or even manipulate the values of inputs/outputs. All event names have the prefix `shiny:`, e.g., `shiny:connected`. We can listen to these events using `.on()` in jQuery, e.g., |
| 12 | + |
| 13 | +```javascript |
| 14 | +$(document).on('shiny:connected', function(event) { |
| 15 | + alert('Connected to the server'); |
| 16 | +}); |
| 17 | +``` |
| 18 | + |
| 19 | +When an event is triggered in Shiny, the `event` object may have some additional properties that can be used to query or modify the information in Shiny, as we will see later in this document. Some events can cancel the process in Shiny, e.g., stop the propogation of an input or output change to the server. Such events include `shiny:inputchanged`, `shiny:message`, `shiny:value`, `shiny:error`, `shiny:updateinput`. To cancel the Shiny process, you can use `event.preventDefault()`, e.g., |
| 20 | + |
| 21 | +```javascript |
| 22 | +// no outputs will be updated since we canceled the output changes |
| 23 | +$(document).on('shiny:value', function(event) { |
| 24 | + event.preventDefault(); |
| 25 | +}); |
| 26 | +``` |
| 27 | + |
| 28 | +All events currently supported in Shiny are listed below. You can find a live example at https://gallery.shinyapps.io/107-events ([source](https://github.com/rstudio/shiny-examples/tree/master/107-events)). |
| 29 | + |
| 30 | +# Initial Connection and Disconnection |
| 31 | + |
| 32 | +The events `shiny:connected` and `shiny:disconnected` are triggered when an initial connection to server is established, and when a session is ended or the connection is lost for some reason, respectively. |
| 33 | + |
| 34 | +A property `socket` in the event object is used to store the web socket that is used to communicate between R and JavaScript. For example, you may query the state of the web socket via `event.socket.readyState`. |
| 35 | + |
| 36 | +# Server Status: Busy/Idle |
| 37 | + |
| 38 | +The event `shiny:busy` is triggered when something is happening on the server (e.g. an observer is running), and the event `shiny:idle` indicates when the server is idle. The event object does not carry any special properties related to Shiny. |
| 39 | + |
| 40 | +# Messages |
| 41 | + |
| 42 | +The event `shiny:inputchanged` is triggered when an input has a new value, e.g., when you click an action button, or type in a text input. The event object has properties `name` (the id of the input), `value` (the value of the input), and `inputType` (the type of the input, e.g. `shiny.action`). |
| 43 | + |
| 44 | +For example, suppose you have a numeric input with id `foo`, you may double its value through this event: |
| 45 | + |
| 46 | +```javascript |
| 47 | +$(document).on('shiny:inputchanged', function(event) { |
| 48 | + if (event.name === 'foo') { |
| 49 | + event.value *= 2; |
| 50 | + } |
| 51 | +}); |
| 52 | +``` |
| 53 | + |
| 54 | +The `shiny:message` is triggered when any messages are received from the server. The event has a property `message`, which is the message object (a JavaScript object). |
| 55 | + |
| 56 | +# Binding/Unbinding Inputs/Outputs |
| 57 | + |
| 58 | +All the events above are triggered on the document. There are a few events triggered on specific HTML elements, including the events in the following sections on input and output elements. |
| 59 | + |
| 60 | +When an input or output is bound to Shiny, the event `shiny:bound` is triggered. Similarly, there is a `shiny:unbound` event after an input/output is unbound. In these events, the event object has properties `binding` (the input/output binding object) and `bindingType` (may be `'input'` or `'output'` depending on the binding is for an input or output). |
| 61 | + |
| 62 | +# Output Events |
| 63 | + |
| 64 | +The `shiny:value` event is triggered when an output receives a value from the server. The event object has three properties: `name` (output id), `value` (output value), and `binding` (output binding). |
| 65 | + |
| 66 | +The `shiny:error` event is triggered when an error is propogated to an output. The event also has three properties like the `shiny:value` event: `name`, `error` (the error message), and `binding`. |
| 67 | + |
| 68 | +Since these events are triggered specifically on an output element, you may add the listener on the output element instead of the document, although the latter also works, e.g. |
| 69 | + |
| 70 | +```javascript |
| 71 | +$('#foo').on('shiny:value', function(event) { |
| 72 | + // append a character string to the output value |
| 73 | + event.value += ' Oh that is nice!'; |
| 74 | +}); |
| 75 | + |
| 76 | +// use event.target to obtain the output element |
| 77 | +$(document).on('shiny:value', function(event) { |
| 78 | + // cancel the output of the element with id 'foo' |
| 79 | + if (event.target.id === 'foo') { |
| 80 | + event.preventDefault(); |
| 81 | + } |
| 82 | +}); |
| 83 | +``` |
| 84 | + |
| 85 | +# Input Events |
| 86 | + |
| 87 | +The `shiny:updateinput` event is triggered when an input is updated, e.g., when you call `updateTextInput()` in R to update the label or value of a text input. The event object has properties `message` (the update message sent from the server) and `binding` (the input binding). |
| 88 | + |
0 commit comments