I am building a rather advanced shiny app. The idea is that users can draw curves by clicking on one plotly plot. Then their input is added to a global data set which in turn is visualized in another plot.
I have a problem with event_data. I need to be able to reset/delete the event_data after I added it’s contents (the result of a click) to global object. Is there any possibility to do that? I found this and this but no easy solutions (also I don’t know javascript).
this was also bothering me for a long time. Today I finally solved this issue. I 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() 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)