【问题标题】:addMarkers function in leaflet not allowing to filter data传单中的 addMarkers 函数不允许过滤数据
【发布时间】:2017-12-10 22:26:16
【问题描述】:

我在 shinyapps 中使用传单库。我正在尝试根据用户输入下拉选择显示我保存为图标的标记。在下面的示例中,我无法根据用户输入过滤掉标记。当我只使用不带光泽的传单包运行时,类似的代码也有效。

下面的示例代码在 Shiny 的 server.r 中。

filteredData <- reactive({
    sampling_data[sampling_data$county == input$county_select, ]
    })

  theData <- filteredData()
    leafletProxy("mapData") %>%
      clearMarkers() %>%
      addMarkers(lng = sampling_data$lon,
                 lat = sampling_data$lat,
                 clusterOptions = markerClusterOptions())
  })

  output$mapData <- renderLeaflet({
    leaflet(sampling_data) %>%
      addProviderTiles(providers$Esri.WorldTopoMap) %>%
      setView(lng = -75, lat = 43, zoom = 6)
  })
  observe({
    theData <- filteredData()
    leafletProxy("mapData") %>%
      clearMarkers() %>%
      addMarkers(lng = sampling_data$lon,
                 lat = sampling_data$lat,
                 clusterOptions = markerClusterOptions())
  }) 

这段代码,没有光泽,工作正常

  subsetData <- sampling_data %>%
  filter(county == "Albany")

  leaflet(subsetData) %>%
  addTiles() %>%
  addMarkers(lng = ~lon,
             lat = ~lat)

【问题讨论】:

  • 你能把代码分享给 addCirceMarkers 吗?
  • 嗨@Seymour。我在提出问题时犯了一个错误。我的意思是说代码可以在没有闪亮的情况下使用传单。但是,在有光泽的情况下运行传单时它会中断。

标签: r leaflet shiny


【解决方案1】:

您只需要 leafletProxy 出现在您的 observe 块中。

library(shiny)
library(dplyr)
library(leaflet)

ui <- fluidPage(

  mainPanel(
    leafletOutput("map"),
    sliderInput("depth", "Depth",
                min = min(quakes$depth),
                max = max(quakes$depth),
                value = c(min(quakes$depth), max(quakes$depth))
    ),
    textOutput("value")
  )

)

server <- function(input, output) {

  filteredData <- reactive({

    quakes %>%
      filter(depth >= input$depth[1] & depth <= input$depth[2])

  })

  output$value <- renderText({paste("min: ", input$depth[1], " max: ", input$depth[2])})

  output$map <- renderLeaflet({

    leaflet(quakes) %>%
      fitBounds(~min(long),
                ~min(lat),
                ~max(long),
                ~max(lat))

  })

  observe({

    leafletProxy("map") %>%
      clearTiles %>%
      clearMarkers %>%
      addTiles %>%
      addMarkers(lng = filteredData()$long, lat = filteredData()$lat)

  })

}

shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2020-10-12
    • 2013-09-12
    • 1970-01-01
    • 2014-07-05
    • 2011-10-12
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多