【问题标题】:Interactive Leaflet with shiny - clearMarkers() and set new ones带有闪亮的交互式传单 - clearMarkers() 并设置新的传单
【发布时间】:2020-10-10 23:21:28
【问题描述】:

我想用 Shiny 创建一个交互式传单地图。创建工作正常,但尽管 selectInput() 发生了变化并单击“开始!”旧标记不会被删除,也不会设置新标记。你能帮帮我吗?

操作系统: Windows 10 64 位 工作室 R 版本:R 版本 4.0.0

geocodes <- data.frame("customer" = c("John", "Ahmet", "Zara"),
                       "longitude" = c(8.71, 8.59, 8.98),
                       "latitude" = c(51.2, 51.3, 51.1))
# UI
ui <- dashboardPage(
  dashboardHeader(title = "CustomerLocation Dashboard"),
  dashboardSidebar(
    
    selectInput("account", label = "Account",
                choices = c(unique(geocodes$customer)),
                multiple = TRUE),
    
    actionButton("go", "Go!")

  ),

  
  dashboardBody(
    tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),
    leafletOutput("mymap", height=1000)
  )
)
  

# server
server <- function(input, output, session){

  reactiveDf <- reactive({
    if(is.null(input$account)){
      geocodes
    } else{
      geocodes[geocodes$customer %in% input$account,]
    }
  })
  
  
  output$mymap <- renderLeaflet({
    leaflet(geocodes) %>%
      addProviderTiles("OpenStreetMap",
                       group = "OpenStreetMap"
      ) %>%
      addAwesomeMarkers(
        lng = ~longitude,
        lat = ~latitude,
        icon = customIcon,
        clusterOptions = markerClusterOptions(),
        label = ~customer,
        popup = ~popup
      )
  })
  
  
  eventReactive(input$go,{
   leafletProxy("mymap", data = reactiveDf()) %>%
      clearShapes() %>%
      clearPopups() %>%
      clearMarkers() %>%
      addAwesomeMarkers(
        data = reactiveDf(),
        lng = ~longitude,
        lat = ~latitude,
        icon = customIcon,
        clusterOptions = markerClusterOptions(),
        label = ~customer,
        popup = ~popup
      )
  })

}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny shinydashboard r-leaflet


    【解决方案1】:

    我发现的三个修复:

    1. c(unique(geocodes$customer)) 将此输入转换为c(1,2,3)。当您稍后尝试通过 c(1,2,3) 将客户名称“John”、“Ahmet”和“Zara”子集时 - 这是不兼容且失败的。
     selectInput("account", label = "Account",
                    choices = c(unique(geocodes$customer)),
                    multiple = TRUE),
    

    改为:

    selectInput("account", label = "Account",
                    choices = unique(geocodes$customer),
                    multiple = TRUE),
    
    1. 不幸的是,我不知道为什么leafletProxy() 中的clearMarkers() 不会像您期望的那样清除标记,而是将原始地图从使用geocodes 渲染更改为reactiveDf()(仍然返回@987654329 @data frame if input$account is null) 似乎解决了这个问题。
    output$mymap <- renderLeaflet({
        leaflet(geocodes) %>%
          addProviderTiles("OpenStreetMap",
                           group = "OpenStreetMap"
          ) %>%
          addAwesomeMarkers(
            lng = ~longitude,
            lat = ~latitude,
            icon = customIcon,
            clusterOptions = markerClusterOptions(),
            label = ~customer,
            popup = ~popup
          )
      })
    

    改为:

    output$mymap <- renderLeaflet({
        leaflet(reactiveDf()) %>%
          addProviderTiles("OpenStreetMap",
                           group = "OpenStreetMap"
          ) %>%
          addAwesomeMarkers(
            lng = ~longitude,
            lat = ~latitude,
            icon = customIcon,
            clusterOptions = markerClusterOptions(),
            label = ~customer,
            popup = ~popup
          )
      })
    

    如果你愿意稍微改变一下架构:

    1. 使用observeEvent根据input$account重新定义geocodes并同时触发leafletProxy()。我想我们在为leafletProxy 创建响应式数据框方面都缺少一些步骤,也许我们无法将数据源从最初的renderLeaflet 完全更改为leafletProxy
    observeEvent(input$go, {
        if(is.null(input$account)){
          geocodes = geocodes
        } else{
         geocodes = geocodes[geocodes$customer %in% input$account,]
        }
     leafletProxy("mymap") %>%
        clearShapes() %>%
        clearPopups() %>%
        clearMarkers() %>%
        addMarkers(
          data = geocodes,
          lng = ~longitude,
          lat = ~latitude,
          label = ~customer,
        )
      }
    )
    

    编辑:另一种选择是为每个标记层定义一个组,并调用 showGroup() 和 hideGroup() 来操作哪些标记是可视化的。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-15
      • 2018-12-03
      • 1970-01-01
      • 2021-08-08
      • 2023-03-24
      • 2017-05-30
      • 2015-12-21
      • 1970-01-01
      相关资源
      最近更新 更多