Skip to content

Can't access event_data("plotly_selected") with geom_tile heatmaps #1809

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

Closed
stevekm opened this issue Jun 22, 2017 · 1 comment
Closed

Can't access event_data("plotly_selected") with geom_tile heatmaps #1809

stevekm opened this issue Jun 22, 2017 · 1 comment

Comments

@stevekm
Copy link

stevekm commented Jun 22, 2017

As per the bug report listed here, we were recently trying to implement interactive heatmaps in Shiny + Plotly, with coupled events such that selecting an area on the heatmap would populate a second plot. At first we tried to use the heatmaply package since it seemed suitable, however we were unable to retrieve event_data("plotly_selected") required for the coupled events.

The heatmaply devs helped narrow the issue down to Plotly itself. It appears that this event data is not available for these types of plots? Here is a working example of the issue, based on the Plotly docs:

library("shiny")
library("plotly")
library("ggplot2")
library("reshape2")

p <- volcano %>%
  melt() %>% 
  ggplot(aes(Var1, Var2, fill = value)) + geom_tile()

p <- ggplotly(p)


# ~~~~~ UI ~~~~~ #
ui <- shinyUI(fluidPage(
  verbatimTextOutput("heatmap_hover"),
  verbatimTextOutput("heatmap_selected"),
  verbatimTextOutput("eventdata"),
  
  # Shiny
  fixedRow(
    column(6, plotlyOutput("volcano_plot", height = "400px"))
  )
))



# ~~~~~ SERVER ~~~~~ #
server <-  shinyServer(function(input, output,session) {
  
  # Heatmap
  output$volcano_plot <- renderPlotly({
    p %>% layout(dragmode = "select")
  })
  
  
  output$heatmap_hover <- renderPrint({
    d <- event_data("plotly_hover")
    if (is.null(d)) "Hover on a point!" else d
  })
  output$heatmap_selected <- renderPrint({
    # d <- event_data("plotly_selected")
    # if (is.null(d)) "Select some points!" else d
    event_data("plotly_selected")
  })
  output$eventdata <- renderPrint({
    str(event_data())
  })
  
})
shinyApp(ui = ui, server = server)

Output:

screen shot 2017-06-22 at 11 37 02 am

As you can see in the screenshot, event_data("plotly_selected") returns only an empty list().

As per the original issue in the heatmaply repo, we resorted to instead using geom_point, and manipulating it to appear like a heatmap. However this solution is very inelegant and seems to have its own bugs.

software versions used:

> sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.5

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

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] reshape2_1.4.2 shiny_1.0.3    plotly_4.7.0   ggplot2_2.2.1 

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.11      compiler_3.4.0    plyr_1.8.4        bindr_0.1         tools_3.4.0       digest_0.6.12    
 [7] jsonlite_1.5      tibble_1.3.3      gtable_0.2.0      viridisLite_0.2.0 pkgconfig_2.0.1   rlang_0.1.1      
[13] crosstalk_1.0.0   yaml_2.1.14       bindrcpp_0.2      dplyr_0.7.1       httr_1.2.1        stringr_1.2.0    
[19] knitr_1.16        htmlwidgets_0.8   grid_3.4.0        glue_1.1.1        data.table_1.10.4 R6_2.2.2         
[25] purrr_0.2.2.2     tidyr_0.6.3       magrittr_1.5      scales_0.4.1      htmltools_0.3.6   assertthat_0.2.0 
[31] mime_0.5          colorspace_1.3-2  xtable_1.8-2      httpuv_1.3.3      labeling_0.3      stringi_1.1.5    
[37] lazyeval_0.2.0    munsell_0.4.3    
@etpinard
Copy link
Contributor

duplicate of #170

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

No branches or pull requests

2 participants