Selaa lähdekoodia

Add event listener to return events back to Shiny

master
Garrick Aden-Buie 6 vuotta sitten
vanhempi
commit
0b4f7ea16f
3 muutettua tiedostoa jossa 23 lisäystä ja 2 poistoa
  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 Näytä tiedosto

@@ -548,3 +548,10 @@ To make this work we will:

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 Näytä tiedosto

@@ -11,7 +11,8 @@ ui <- fluidPage(
),
# Show a plot of the generated distribution
mainPanel(
frappeCharts::frappeChartOutput("chart")
frappeCharts::frappeChartOutput("chart"),
verbatimTextOutput("selected")
)
)
)
@@ -42,6 +43,10 @@ server <- function(input, output, session) {
observe({
updateFrappeChart("chart", data())
})

output$selected <- renderPrint({
input$chart_selected
})
}

# Run the application

+ 10
- 1
inst/htmlwidgets/frappeChart.js Näytä tiedosto

@@ -36,6 +36,13 @@ HTMLWidgets.widget({
x.data = prepareChartData(x.data)

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) {
@@ -61,6 +68,8 @@ HTMLWidgets.widget({
if (HTMLWidgets.shinyMode) {
Shiny.addCustomMessageHandler('frappeCharts:update', function({id, data}) {
let el = document.getElementById(id)
el.widget.update(data)
if (el.widget) {
el.widget.update(data)
}
})
}

Loading…
Peruuta
Tallenna