Mark Mark - 10 days ago 5
R Question

R Shiny get click information from scatterD3

I have a scatterd3 (package scatterd3) plot in a shiny app and I can't figure out how to pass information after clicking a plotted point back to shiny though. This website seems to explain it but I just can't get it:
https://cran.r-project.org/web/packages/scatterD3/vignettes/introduction.html#javascript-callback-on-clicking-point

scatterD3(data = mtcars, x = wt, y = mpg,
click_callback = "function(id, index) {
if(id && typeof(Shiny) != 'undefined') {
Shiny.onInputChange(id + '_selected', index);
}
}")


Do you know where the information on click is stored? I assumed I could call it in shiny gui with something like this:

verbatimTextOutput("scatterPlot$index")


Any ideas?
Cheers

Answer

The click_callback JavaScript function is called with two arguments : the plot unique id, and the clicked point index. By using Shiny.onInputChange, you can bind a Shiny input slot to a value and get back the data.

For example, with the following click_callback function :

scatterD3(data = mtcars, x = wt, y = mpg,
  click_callback = "function(id, index) {
  if(id && typeof(Shiny) != 'undefined') {
      Shiny.onInputChange('selected_point', index);
  }
}")

You should be able to do something like :

verbatimTextOutput(paste("Index of clicked point : ", input$selected_point))

This is quite a recent feature in scatterD3, so in case of bug or missing feature, don't hesitate to open an issue on Github.

Comments