R绘图返回地图上选定线的event_data信息

时间:2019-02-06 19:27:43

标签: r shiny plotly r-plotly

我正在尝试使用event_data在绘制地图下方的表中提供额外信息。

我发现了这个:https://community.plot.ly/t/how-to-return-the-same-event-data-information-for-selected-points-even-after-modifying-the-data/5847。通过将key列添加到aes来使用ggplot的一个好例子:

ggplot(mtcars, aes(mpg, wt, col = I(col), key = key)) 

但是,如何才能在散点图中添加键列,以便在event_data中弹出键?任何帮助将不胜感激。

R闪亮代码:

library(plotly)
library(shiny)

ui <- fluidPage(
  plotlyOutput("plot"),
  verbatimTextOutput("event")
)
server <- function(input, output, session) {

  #df sample using dput
  df <- structure(list(col_group = structure(1:8, .Label = c("1", "2", 
                                                             "3", "4", "5", "6", "7", "8"), class = "factor"), color = structure(c(4L, 
                                                                                                                                   6L, 7L, 8L, 5L, 3L, 2L, 1L), .Label = c("#1A9850", "#66BD63", 
                                                                                                                                                                           "#A6D96A", "#D73027", "#D9EF8B", "#F46D43", "#FDAE61", "#FEE08B"
                                                                                                                                   ), class = "factor"), geometry = structure(list(structure(c(4.52343199617246, 
                                                                                                                                                                                               4.52324233358547, 4.52267342343957, 4.52224662532908, 4.52210428346744, 
                                                                                                                                                                                               4.52200925015845, 4.52192088448668, 4.52180475361204, 4.52172945325391, 
                                                                                                                                                                                               4.52168196905882, 4.5215535740952, 4.52095980475523, 4.52076420632298, 
                                                                                                                                                                                               4.52062274547478, 51.9453195440486, 51.9453722803981, 51.945526317454, 
                                                                                                                                                                                               51.9456480852256, 51.9456928353329, 51.9457296098645, 51.9457705951341, 
                                                                                                                                                                                               51.9458530114811, 51.945914910501, 51.9459312169901, 51.9459510896027, 
                                                                                                                                                                                               51.9459966849614, 51.9460077291392, 51.9460066867221), .Dim = c(14L, 
                                                                                                                                                                                                                                                               2L), class = c("XY", "LINESTRING", "sfg")), structure(c(4.4964540696277, 
                                                                                                                                                                                                                                                                                                                       4.49696710232736, 51.9086692611627, 51.9084484303039), .Dim = c(2L, 
                                                                                                                                                                                                                                                                                                                                                                                       2L), class = c("XY", "LINESTRING", "sfg")), structure(c(4.13635479859532, 
                                                                                                                                                                                                                                                                                                                                                                                                                                               4.13644010080625, 51.975098751212, 51.9751715711302), .Dim = c(2L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              2L), class = c("XY", "LINESTRING", "sfg")), structure(c(4.47801457239531, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      4.47834576882542, 51.9300740588744, 51.9304218318716), .Dim = c(2L, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      2L), class = c("XY", "LINESTRING", "sfg")), structure(c(4.45974369011875, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              4.46029492493512, 4.46033964902157, 51.9290774018138, 51.9284345596986, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              51.9283809798498), .Dim = 3:2, class = c("XY", "LINESTRING", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       "sfg")), structure(c(4.43886518844695, 4.43891013390463, 4.43910559159559, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            4.43913561800293, 51.93455577157, 51.9344932127658, 51.9341891712133, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            51.9341444695365), .Dim = c(4L, 2L), class = c("XY", "LINESTRING", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           "sfg")), structure(c(4.54844667292407, 4.55002805772657, 51.9658870347267, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                51.9661409927825), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               "sfg")), structure(c(4.47522875290306, 4.47598748813623, 51.9347985281278, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    51.9353886781931), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   "sfg"))), class = c("sfc_LINESTRING", "sfc"), precision = 0, bbox = structure(c(xmin = 4.13635479859532, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ymin = 51.9084484303039, xmax = 4.55002805772657, ymax = 51.9751715711302
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ), class = "bbox"), crs = structure(list(epsg = 4326L, proj4string = "+proj=longlat +datum=WGS84 +no_defs"), class = "crs"), n_empty = 0L), 
                       key = c("1", "8000", "10000", "12000", "14000", "16000", 
                               "18000", "22000")), row.names = c(1L, 8000L, 10000L, 12000L, 
                                                                 14000L, 16000L, 18000L, 22000L), class = c("sf", "data.frame"
                                                                 ), sf_column = "geometry", agr = structure(c(col_group = NA_integer_, 
                                                                                                              color = NA_integer_, key = NA_integer_), .Label = c("constant", 
                                                                                                                                                                  "aggregate", "identity"), class = "factor"))
  #plotly
  output$plot <- renderPlotly({
    plot_geo(df, type = "scattergl", mode = "lines",
             color = ~col_group, colors = rev(RColorBrewer::brewer.pal(8, name = "RdYlGn"))
    )
  })

  #event_data
  output$event <- renderPrint({
    d <- event_data("plotly_click")
    if (is.null(d)) "Click events appear here (double-click to clear)" else {
      df_select <- df %>% tibble::rownames_to_column() %>% filter(col_group==d$curveNumber-7) %>% filter(row_number()==d$pointNumber+1)
      browser()
      print(df_select)
    }
  })

}
shinyApp(ui, server)

1 个答案:

答案 0 :(得分:1)

使用customdata和更新的plotly软件包解决。也许对其他人有用:

- name: Execute the script
   command: sh /tmp/shutdown.sh log_path log_name log_dir