【问题标题】:R Shiny/Leaflet Jump to filtered MarkersR Shiny/Leaflet 跳转到过滤标记
【发布时间】:2021-10-24 23:22:55
【问题描述】:

我正在一个闪亮的应用程序中绘制瑞士的自行车事故。按州过滤事故,但我想在每次选择新州时重新定位地图。我认为 flyTo() 可以解决问题,但我无法让它发挥作用。

if(!require('leaflet')){install.packages('leaflet'); library('leaflet')}
if(!require('shiny')){install.packages('shiny'); library('shiny')}
if(!require('tidyverse')){install.packages('tidyverse'); library('tidyverse')}  


veloaua <- read_csv(file = 'https://raw.githubusercontent.com/nicoschreibt/velovaua/master/veloaua_github.csv?token=AP64ETIQHSXQADG2GKFM6B3BEVLM6')
    veloaua <- veloaua[sample(1:length(veloaua$AccidentType), size = 100),]

ui <- fluidPage(
        selectInput(inputId = "kant", 
                    label = "Welchen Kanton willst du sehen?", 
                    choices = unique(veloaua$CantonCode)),
      leafletOutput("mymap")
    )

server <- function(input, output, session) {
  
      output$mymap <- renderLeaflet({
      leaflet() %>%
      addProviderTiles(providers$Stamen.Toner, options = providerTileOptions(minZoom = 8, maxZoom = 20))%>%
      clearBounds() %>%
      addMarkers(data = veloaua,
                  lng = ~wgs84_e, 
                 lat = ~wgs84_n, 
                 icon = icons, 
                 popup = veloaua$poptext,
                 group = "main")
      })
      
      observeEvent(input$kant, {
        leafletProxy("mymap")%>%
          clearGroup("main") %>%
          addMarkers(data = veloaua[veloaua$CantonCode == input$kant,],
                     lng = ~wgs84_e, 
                     lat = ~wgs84_n,
                     popup = ~poptext,
                     group = "main",
                     clusterOptions = markerClusterOptions(
                       removeOutsideVisibleBounds = TRUE, 
                       showCoverageOnHover = FALSE,
                       disableClusteringAtZoom = 15)) %>%
          leaflet::flyTo(map = "mymap",
                         lng =  veloaua$wgs84_e[veloaua$CantonCode == input$kant], 
                         lat = veloaua$wgs84_n[veloaua$CantonCode == input$kant])
      }
                   )
}
shinyApp(ui = ui, server = server)

【问题讨论】:

  • 请提供reproducible minimal example。特别是,提供一些示例数据,例如与dput()
  • 您是否尝试过管道到 setView()?您可以设置地图的经纬度中心。
  • 好点。也许你可以让它像这样工作。
  • 我不想要地图的中心,而是我设置的标记之一。这就是它的全部意义 =)

标签: r shiny leaflet


【解决方案1】:

找到解决办法:

ui <- fluidPage(
    selectInput(inputId = "kant", 
                label = "Welchen Kanton willst du sehen?", 
                choices = unique(veloaua$CantonCode)),
  leafletOutput("mymap")
)

server <- function(input, output, session) {
  
  
  center <- reactive({
    subset(x = veloaua, CantonCode == input$kant) 
    # or whatever operation is needed to transform the selection 
    # to an object that contains lat and long
  })
  
      output$mymap <- renderLeaflet({
      leaflet() %>%
      addProviderTiles(providers$Stamen.Toner, options = providerTileOptions(minZoom = 8, maxZoom = 20))%>%
      clearBounds() %>%
      addMarkers(data = veloaua,
                  lng = ~wgs84_e, 
                 lat = ~wgs84_n, 
                 icon = icons, 
                 popup = veloaua$poptext,
                 group = "main")
      })
      
      observeEvent(input$kant, {
        leafletProxy("mymap")%>%
          clearGroup("main") %>%
          addMarkers(data = veloaua[veloaua$CantonCode == input$kant,],
                     lng = ~wgs84_e, 
                     lat = ~wgs84_n,
                     popup = ~poptext,
                     group = "main",
                     clusterOptions = markerClusterOptions(
                       removeOutsideVisibleBounds = TRUE, 
                       showCoverageOnHover = FALSE,
                       disableClusteringAtZoom = 15)) %>%
          setView(lng = center()$wgs84_e[1], lat = center()$wgs84_n[1], zoom = 15)
      }
                   )
}
shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2016-02-09
    • 2022-01-08
    • 2023-02-07
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    相关资源
    最近更新 更多