Mark
Mark

Reputation: 606

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

Upvotes: 1

Views: 398

Answers (2)

Mark
Mark

Reputation: 606

juba's answer helped a lot, see above. I wanted to update a numericInput field, so went about it like this in my server:

observe({
  click_index <- input$selected_point
  updateNumericInput(session, "numeric1", value=click_index)
})

Where my numericInput in my gui was:

numericInput("numeric1", "Clicked point index :", value=0)

Upvotes: 1

juba
juba

Reputation: 49033

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.

Upvotes: 3

Related Questions