Parcourir la source

Add event listener to return events back to Shiny

master
Garrick Aden-Buie il y a 6 ans
Parent
révision
0b4f7ea16f
3 fichiers modifiés avec 23 ajouts et 2 suppressions
  1. +7
    -0
      dev/dev.Rmd
  2. +6
    -1
      dev/shiny/app.R
  3. +10
    -1
      inst/htmlwidgets/frappeChart.js

+ 7
- 0
dev/dev.Rmd Voir le fichier



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`.

+ 6
- 1
dev/shiny/app.R Voir le fichier

), ),
# 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

+ 10
- 1
inst/htmlwidgets/frappeChart.js Voir le fichier

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)
}
}) })
} }

Chargement…
Annuler
Enregistrer