Skip to content

event_data doesn't work with scatterplot (type = "scattergl") #624

Closed
@naikai

Description

@naikai

Hello,

I was trying to follow the examples from these two links:
Shiny-coupled-events
#596

Everything works fine until I changed the type = 'scatter' to type = 'scattergl'
I lost the "box select" option after the change.
In comparison, it work fine for ggplot + ggplotly and I can still get the data info from the selected box area.
I would like to use the additional features from plot_ly instead of switching back to ggplot
Thank you for your help.

Here is a minimal example code

library(shiny)
library(plotly)

adds <- mtcars[1:5, ]
adds[, c(1, 6)] <- NA
rownames(adds) <- paste(rownames(adds), "(NA)")
some_null_cars <- rbind(mtcars, adds)

ui <- fluidPage(
  radioButtons("plotType", "Plot Type:", choices = c("ggplotly", "plotly")),
  plotlyOutput("plot"),
  verbatimTextOutput("brush")
)

server <- function(input, output, session) {

  output$plot <- renderPlotly({
    # use the key aesthetic/argument to help uniquely identify selected observations
    key <- row.names(some_null_cars)
    if (identical(input$plotType, "ggplotly")) {
      p <- ggplot(some_null_cars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) +
        geom_point()
      ggplotly(p) %>% layout(dragmode = "select")
    } else {
      plot_ly(some_null_cars, x = mpg, y = wt, key = key, mode = "markers", type = "scattergl") %>%
        layout(dragmode = "select")
    }
  })

  output$brush <- renderPrint({
    d <- event_data("plotly_selected")
    if (is.null(d)) "Click and drag events (i.e., select/lasso) appear here (double-click to clear)" else d
  })
}

shinyApp(ui, server)

Here is my session info:

> sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.5 (El Capitan)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] plotly_3.6.4  ggplot2_2.1.0 shiny_0.13.2 

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.5      magrittr_1.5     munsell_0.4.3    colorspace_1.2-6 xtable_1.8-2    
 [6] R6_2.1.2         httr_1.1.0       plyr_1.8.3       dplyr_0.4.3      tools_3.3.0     
[11] parallel_3.3.0   grid_3.3.0       gtable_0.2.0     DBI_0.4-1        htmltools_0.3.5 
[16] yaml_2.1.13      digest_0.6.9     assertthat_0.1   gridExtra_2.2.1  tidyr_0.4.1     
[21] base64enc_0.1-3  htmlwidgets_0.6  viridis_0.3.4    rsconnect_0.4.3  mime_0.4        
[26] labeling_0.3     scales_0.4.0     jsonlite_0.9.20  httpuv_1.3.3    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions