| 1. Write a user-friendly wrapper around `sendCustomMessage` called `updateFrappeChart()` | 1. Write a user-friendly wrapper around `sendCustomMessage` called `updateFrappeChart()` | ||||
| 1. Now add an event listener to send chart navigation back to Shiny | |||||
| Attach the event listener during `renderValue()` and watch for the `data-select` event. | |||||
| Use the `el.id` to create a new id, like `el.id + '_selected'`. | |||||
| Send back `index` and `values` from the event. | |||||
| Add `verbatimTextOutput('selected')` to show `input$chart_selected`. |
| ), | ), | ||||
| # Show a plot of the generated distribution | # Show a plot of the generated distribution | ||||
| mainPanel( | mainPanel( | ||||
| frappeCharts::frappeChartOutput("chart") | |||||
| frappeCharts::frappeChartOutput("chart"), | |||||
| verbatimTextOutput("selected") | |||||
| ) | ) | ||||
| ) | ) | ||||
| ) | ) | ||||
| observe({ | observe({ | ||||
| updateFrappeChart("chart", data()) | updateFrappeChart("chart", data()) | ||||
| }) | }) | ||||
| output$selected <- renderPrint({ | |||||
| input$chart_selected | |||||
| }) | |||||
| } | } | ||||
| # Run the application | # Run the application |
| x.data = prepareChartData(x.data) | x.data = prepareChartData(x.data) | ||||
| chart = new frappe.Chart(el, x) | chart = new frappe.Chart(el, x) | ||||
| if (HTMLWidgets.shinyMode && x.isNavigable) { | |||||
| el.addEventListener('data-select', function(ev) { | |||||
| let {index, values} = ev | |||||
| Shiny.setInputValue(el.id + '_selected', {index, values}) | |||||
| }) | |||||
| } | |||||
| }, | }, | ||||
| resize: function(width, height) { | resize: function(width, height) { | ||||
| if (HTMLWidgets.shinyMode) { | if (HTMLWidgets.shinyMode) { | ||||
| Shiny.addCustomMessageHandler('frappeCharts:update', function({id, data}) { | Shiny.addCustomMessageHandler('frappeCharts:update', function({id, data}) { | ||||
| let el = document.getElementById(id) | let el = document.getElementById(id) | ||||
| el.widget.update(data) | |||||
| if (el.widget) { | |||||
| el.widget.update(data) | |||||
| } | |||||
| }) | }) | ||||
| } | } |