【问题标题】:R plotly `event_data` does not return `key` / `customdata` in a shiny appR plotly`event_data`不会在闪亮的应用程序中返回`key`/`customdata`
【发布时间】:2021-04-12 21:38:29
【问题描述】:

我想根据图表中的点选择过滤表格并仅显示相关数据。 我正在尝试使用event_data 来过滤表格,使用情节和闪亮。 这个例子是我从官方书籍link复制过来的

这不会在我的电脑上呈现或显示customdata,但它适用于书中给出的链接。 link to the 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 实例有什么问题。下面是我的会话信息。关于可能是什么问题的任何建议?

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 年 4 月 12 日创建

【问题讨论】:

    标签: r shiny r-plotly ggplotly


    【解决方案1】:

    我相信这是因为您正在 R Studio 内置查看器中运行闪亮的应用程序。更改设置以在浏览器中运行闪亮的应用程序,它应该可以工作,因为情节事件信息来自客户端(浏览器)

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2017-02-09
    • 2020-12-29
    • 2019-08-15
    • 2018-05-22
    • 2016-12-29
    • 2016-12-20
    • 2018-07-14
    • 2020-03-12
    • 1970-01-01
    相关资源
    最近更新 更多