[英]R plotly `event_data` does not return `key` / `customdata` in a shiny app
我想過濾表格並根據圖表中的點選擇僅顯示相關數據。 我正在嘗試使用event_data
來過濾使用 plotly 和 shiny 的表。我從官方書籍鏈接復制了這個例子
這不會在我的customdata
上呈現或顯示自定義數據,但它適用於書中給出的鏈接。 鏈接到 shiny vis
library(shiny)
library(plotly)
ui <- fluidPage(
radioButtons("plotType", "Plot Type:", choices = c("ggplotly", "plotly")),
plotlyOutput("plot"),
verbatimTextOutput("hover"),
verbatimTextOutput("click"),
verbatimTextOutput("brushing"),
verbatimTextOutput("selecting"),
verbatimTextOutput("brushed"),
verbatimTextOutput("selected")
)
server <- function(input, output) {
nms <- row.names(mtcars)
output$plot <- renderPlotly({
p <- if (identical(input$plotType, "ggplotly")) {
ggplotly(ggplot(mtcars, aes(x = mpg, y = wt, customdata = nms)) + geom_point())
} else {
plot_ly(mtcars, x = ~mpg, y = ~wt, customdata = nms)
}
p %>%
layout(dragmode = "select") %>%
event_register("plotly_selecting")
})
output$hover <- renderPrint({
d <- event_data("plotly_hover")
if (is.null(d)) "Hover events appear here (unhover to clear)" else d
})
output$click <- renderPrint({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)" else d
})
output$brushing <- renderPrint({
d <- event_data("plotly_brushing")
if (is.null(d)) "Brush extents appear here (double-click to clear)" else d
})
output$selecting <- renderPrint({
d <- event_data("plotly_selecting")
if (is.null(d)) "Brush points appear here (double-click to clear)" else d
})
output$brushed <- renderPrint({
d <- event_data("plotly_brushed")
if (is.null(d)) "Brush extents appear here (double-click to clear)" else d
})
output$selected <- renderPrint({
d <- event_data("plotly_selected")
if (is.null(d)) "Brushed points appear here (double-click to clear)" else d
})
}
shinyApp(ui, server)
我不確定我的 R 實例有什么問題。下面是我的 session 信息。 關於可能是什么問題的任何建議?
sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
Random number generation:
RNG: Mersenne-Twister
Normal: Inversion
Sample: Rounding
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] reprex_0.3.0 shiny_1.6.0 plotly_4.9.3 ggplot2_3.3.3
loaded via a namespace (and not attached):
[1] tinytex_0.26 tidyselect_1.1.0 xfun_0.22 bslib_0.2.4 purrr_0.3.4
[6] colorspace_1.4-1 vctrs_0.3.6 generics_0.1.0 htmltools_0.5.1.1 viridisLite_0.3.0
[11] yaml_2.2.1 rlang_0.4.10 pillar_1.4.7 later_1.1.0.1 jquerylib_0.1.3
[16] glue_1.4.2 withr_2.4.1 DBI_1.1.1 lifecycle_1.0.0 munsell_0.5.0
[21] gtable_0.3.0 htmlwidgets_1.5.3 evaluate_0.14 labeling_0.4.2 knitr_1.31
[26] callr_3.5.1 fastmap_1.0.1 ps_1.3.4 httpuv_1.5.4 crosstalk_1.1.1
[31] Rcpp_1.0.5 clipr_0.7.0 xtable_1.8-4 scales_1.1.1 promises_1.1.1
[36] DT_0.17 cachem_1.0.4 jsonlite_1.7.1 fs_1.5.0 mime_0.9
[41] digest_0.6.25 processx_3.4.4 dplyr_1.0.3 grid_4.0.2 cli_2.3.0
[46] tools_4.0.2 magrittr_2.0.1 sass_0.3.1 lazyeval_0.2.2 tibble_3.0.3
[51] whisker_0.4 crayon_1.4.1 tidyr_1.1.2 pkgconfig_2.0.3 ellipsis_0.3.1
[56] rsconnect_0.8.17 data.table_1.13.0 assertthat_0.2.1 rmarkdown_2.7 httr_1.4.2
[61] R6_2.5.0 compiler_4.0.2
由reprex package (v0.3.0) 創建於 2021-04-12
我相信這是因為您正在 R 工作室內置查看器中運行 shiny 應用程序。 更改設置以在瀏覽器中運行 shiny 應用程序,它應該可以作為 plotly 事件信息來自客戶端(瀏覽器)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.