plotly event_data:工具提示和plotly_click事件结果之间不匹配

时间:2019-07-15 10:04:24

标签: r ggplot2 shiny plotly

我的目标是制作一个热图,并在此基础上加点云。当我单击热图中的一个正方形时,我想捕获有关该正方形的信息以供以后使用。 但是,当我将鼠标悬停在正方形上时所显示的结果与单击一个正方形时所显示的结果之间不一致。

this thread之后,我添加了key列,这应该有助于我识别tile_cars数据框中的行。

这是示例闪亮的应用程序。


library(plotly)
library(dplyr)
library(shiny)

tile_cars <- expand.grid(
  drat = 2:5
  , wt = 1:6
) %>% 
  mutate(
    val = rnorm(nrow(.))
    , key = row_number()
    )

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

server <- function(input, output, session) {
  output$plot <- renderPlotly({
    p <- ggplot() +
      geom_point(data = mtcars, aes(x = drat, y = wt)) +
      geom_tile(data = tile_cars, aes(x = drat, y = wt, fill = val, key = key))
    ggplotly(p)
  })

  output$selection <- renderPrint({
    s <- event_data("plotly_click")
    if (length(s) == 0) {
      "Click on a cell in the heatmap"
    } else {
      cat("You selected: \n\n")
       as.list(s)

    }
  })
}

shinyApp(ui, server)

当我将鼠标悬停在第一行第一列的第二行上时,得到的键=17。如果单击相同的单元格,则逐字记录的文本报告的键为“ 5”。 另外,某些单元格不会报告点击的任何键(例如第一行,第二列)。

> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] plotly_4.8.0     bindrcpp_0.2.2   stringr_1.3.1    ggplot2_3.1.0    lubridate_1.7.4 
[6] dplyr_0.7.8      RODBC_1.3-15     shiny_1.2.0     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0          pillar_1.3.1        later_0.7.5         plyr_1.8.4         
 [5] bindr_0.1.1         tools_3.3.2         digest_0.6.18       viridisLite_0.3.0  
 [9] jsonlite_1.6        tibble_2.0.0        gtable_0.2.0        pkgconfig_2.0.2    
[13] rlang_0.4.0         cli_1.0.1           rstudioapi_0.8      crosstalk_1.0.0    
[17] yaml_2.2.0          httr_1.4.0          withr_2.1.2         htmlwidgets_1.3    
[21] grid_3.3.2          tidyselect_0.2.5    glue_1.3.0          data.table_1.11.8  
[25] R6_2.3.0            fansi_0.4.0         tidyr_0.8.2         purrr_0.2.5        
[29] magrittr_1.5        scales_1.0.0        promises_1.0.1      htmltools_0.3.6    
[33] randomForest_4.6-14 assertthat_0.2.0    mime_0.6            colorspace_1.3-2   
[37] xtable_1.8-3        httpuv_1.4.5.1      labeling_0.3        utf8_1.1.4         
[41] stringi_1.2.4       lazyeval_0.2.1      munsell_0.5.0       crayon_1.3.4       
[45] Cairo_1.5-9    

0 个答案:

没有答案