Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Shift on event_data("plotly_click") only stores last key #1401

Open
trafficonese opened this issue Nov 13, 2018 · 2 comments
Open

Using Shift on event_data("plotly_click") only stores last key #1401

trafficonese opened this issue Nov 13, 2018 · 2 comments

Comments

@trafficonese
Copy link

trafficonese commented Nov 13, 2018

When using the SHIFT-key on a plotly_click event, it returns only the last clicked key, whereas using SHIFT on plotly_selected stores all selected keys.

Is it possible to adapt the event_data("plotly_click") to be consistent with the highlighting and return all clicked/highlighted elements?

Or can I deactivate SHIFT for click-events alltogether and leave it activated for selection-events?

clickselect

Shiny-App:

library(shiny)
library(ggplot2)
library(plotly)

dfN <- data.frame(
  time_stamp = seq.Date(as.Date("2018-04-01"), as.Date("2018-07-30"), 1),
  val = runif(121, 100,1000),
  col = "green", stringsAsFactors = F
)


ui <- fluidPage(
  plotlyOutput("plot"),
  verbatimTextOutput("clicked"),
  verbatimTextOutput("selection")
)

server <- function(input, output, session) {
  output$plot <- renderPlotly({
    key <- highlight_key(dfN)
    p <- ggplot() +
      geom_col(data = key, aes(x = plotly:::to_milliseconds(time_stamp), y = val, fill=I(col))) +
      theme(legend.position="none")
    
    ggplotly(p, source = "Src") %>% layout(xaxis = list(tickval = NULL, ticktext = NULL, type = "date")) %>% 
      highlight(selectize=F, off = "plotly_doubleclick", on = "plotly_click", color = "blue",
                opacityDim = 0.5, selected = attrs_selected(opacity = 1))
  })
    

  output$clicked <- renderPrint({
    s <- event_data("plotly_click", source = "Src")
    s
  })
  output$selection <- renderPrint({
    s <- event_data("plotly_selected", source = "Src")
    s
  })
}

shinyApp(ui, server)
@cpsievert
Copy link
Collaborator

You raise a great point with this example and I don't think there currently is a great way adapt 'plotly_click' to accumulate data by holding shift. I will point out though that event_data() is designed to work both with and without highlight_key(), so it tries to not be too opinionated about what you want to do with it.

I think there may be cases where, even if someone happens to be holding shift, you don't want 'plotly_click' to accumulate, so it seems safer to just expose another input event, say 'plotly_shift_click', that fires the accumulated data. I'll take a stab at an implementation in #1392

@cpsievert
Copy link
Collaborator

And come to think of it, 'plotly_hover' suffers from the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants