Removing plotly click event data

前端 未结 2 1819
天命终不由人
天命终不由人 2020-12-20 17:09

I am designing a Shiny app which contains a plotly scatter plot. I would like for the user to be able to click on the graph to record an event using the e

2条回答
  •  萌比男神i
    2020-12-20 17:58

    The previous answer partially solves the problem, however, the user cannot click on the same plotly marker again, at least no update is triggered. That problem can be tackled by manually resetting the source of event_data("plotly_click") withshinyjs:

    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)
    

提交回复
热议问题