Coupled events with Shiny and Plotly in R, Resetting Click Event Data Once New Plot Is Rendered


#1

For R Plotly being used with R Shiny

Would anyone be able to discuss how to get plot click event data (from the evet_data() function) to reset when the plot is re-rendered? In the work that I’m doing, it doesn’t seem to be happening automatically.

I can put together some example code, but I’m first wondering if there is anyone that would be willing to discuss it.

Thanks!
-Jeff Swartzel


Reseting click-events
#2

Jeff; I am running into the same issue. Unable to help you at this moment. Will let you know if I find something. Have you had any breakthrough?

Thanks

Sri


#3

I haven’t been able to figure anything else out yet. I’m sure that there’s a way to do it in a round-about way, but I haven’t figured it out yet.

Jeff Swartzel | Procter & Gamble | QS Informatics | mobile: 937-626-0593
[eMail_Signature_Logo]


#4

Hey! Has there been any progress on this? Best!


#5

I have solved that just today, and have posted the answer as an answer to my own stackoverflow question. Here is a working minimal example:

library(shiny)
library(plotly)
library(shinyjs)

ui <- shinyUI(
  fluidPage(
    useShinyjs(),
    # code to reset plotlys event_data("plotly_click", source="A") to NULL -> executed upon action button click
    # note that "A" needs to be replaced with plotly source string if used
    extendShinyjs(text = "shinyjs.resetClick = function() { Shiny.onInputChange('.clientValue-plotly_click-A', 'null'); }"),
    actionButton("reset", "Reset plotly click value"),
    plotlyOutput("plot"),
    verbatimTextOutput("clickevent")
  )
)


server <- shinyServer(function(input, output) {

  output$plot <- renderPlotly({
    plot_ly(mtcars, x=~cyl, y=~mpg)
  })
  
  output$clickevent <- renderPrint({
    event_data("plotly_click")
  })

  observeEvent(input$reset, {
    js$resetClick()
  })
})

shinyApp(ui, server)