0

I want to filter a table and show only relevant data based on selection of points in a chart. I am trying to use event_data to filter the table using plotly and shiny. I copied this example from the official book link

This does not render or show me the customdata on my pc, however it works on the link given in the book. 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)

This is how it looks on my computer

This is how it looks in the documentation

I am not sure what is wrong with my instance of R. Given below is my session info. Any advise on what could be the problem ?

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  

Created on 2021-04-12 by the reprex package (v0.3.0)

ok1more
  • 779
  • 6
  • 15

1 Answers1

0

I believe this is because you are running the shiny app in the R studio inbuilt viewer. Change the setting to run the shiny app in a browser and it should work as plotly event information comes from client (browser)

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 09 '22 at 03:01