Removing plotly click event data

前端 未结 2 1816
天命终不由人
天命终不由人 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条回答
  •  独厮守ぢ
    2020-12-20 18:08

    In your example, e is just defined in the renderPrint and in the observeEvent and not globally so even if e is changed in the observeEvent, it does not trigger anything in the renderPrint.

    You can use reactiveValues for this:

    data <- reactiveValues(e=NULL)
    
      observe({
        data$e <- event_data("plotly_click")
      })
    
      output$plotVal <- renderPrint({
        e <- data$e
        if (is.null(e)) {
          NULL
        } else {
          e
        }
      })
    
      observeEvent(input[["clearEvent"]], {
        data$e <- NULL
      })
    

    data$e is changed whenever the user click the plot or the button, and since there is a dependency on data$e in the renderPrint, that gets updated whenever data$e is changed.

提交回复
热议问题