plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

Access persistent click/hover data in shiny, addresses #1401

Open cpsievert opened this issue 5 years ago • 1 comments

See #1401 for more context

Review questions

  • [ ] Is it safe to remember event data by attaching an attribute to graphDiv in this way? Should try this out in conjunction with plotlyProxy().
  • [ ] Should we worry about emitting redundant data in the persistent case? Or should this be the responsibility of the user?
  • [ ] Should we worry that the persistent hover data isn't cleared when unhovering after a shift?

Testing notes

Install (devtools::install_github("ropensci/plotly#1409")), run this app, and make sure the event data is what you'd expect.

library(shiny)
library(plotly)

ui <- fluidPage(
  plotlyOutput("plot"),
  verbatimTextOutput("click"),
  verbatimTextOutput("shift_click"),
  verbatimTextOutput("hover"),
  verbatimTextOutput("shift_hover")
)

server <- function(input, output, session) {
  output$plot <- renderPlotly({
    plot_ly(x = 1:10, y = 1:10, key = LETTERS[1:10])
  })
  
  output$click <- renderPrint({
    d <- event_data("plotly_click")
    if (is.null(d)) "Transient click data" else d
  })
  output$shift_click <- renderPrint({
    d <- event_data("plotly_click_persist_on_shift")
    if (is.null(d)) "Persistent click data" else d
  })
  output$hover <- renderPrint({
    d <- event_data("plotly_hover")
    if (is.null(d)) "Transient hover data" else d
  })
  output$shift_hover <- renderPrint({
    d <- event_data("plotly_hover_persist_on_shift")
    if (is.null(d)) "Persistent hover data" else d
  })
}

shinyApp(ui, server)

cpsievert avatar Nov 15 '18 23:11 cpsievert

Shouldn't a doubleclick clear the Shift selections? Now the selection is only cleared, by clicking a new point/bar.

It also doesnt always reflect the highlighted elements, as Shift must be pressed already when clicking the first element. The "usual" behaviour would be to click the first without Shift and then to add more into the selection, hold Shift and click another point which will be added to the current selection.

The Box/Lasso-selection behave like the "usual" way.

Otherwise nice work 👍

trafficonese avatar Feb 08 '19 17:02 trafficonese