过滤传单中的传单地图数据

时间:2018-05-02 06:27:17

标签: r filter shiny leaflet r-leaflet

我在使用传单地图设置这个闪亮的时候遇到了麻烦。我的original post有两个问题,建议我应该开一个新帖来解决我的第二个问题:如何在我按速度过滤后让地图显示我的更新数据;我的表格会更新我是否改变"速度"或地图边界,但传单地图不会根据速度过滤器输入更新点。

可重复使用的代码

library(shiny)
library(magrittr)
library(leaflet)
library(DT)

ships <-
  read.csv(
    "https://raw.githubusercontent.com/Appsilon/crossfilter-demo/master/app/ships.csv"
  )

ui <- shinyUI(fluidPage(
  titlePanel("Filter"),
  sidebarLayout(
    sidebarPanel(width = 3,
                 numericInput(
                   "speed_f", label = h5("Ship's Speed"), value = 100
                 )),
    mainPanel(tabsetPanel(
      type = "tabs",
      tabPanel(
        "Leaflet",
        leafletOutput("leafletmap", width = "350px"),
        dataTableOutput("tbl")
      )
    ))
  )
))

server <- function(input, output) {
  in_bounding_box <- function(data, lat, long, bounds, speed) {
    data %>%
      dplyr::filter(
        lat > bounds$south &
          lat < bounds$north &
          long < bounds$east & long > bounds$west & 
          speed > input$speed_f
      )
  }

  output$leafletmap <- renderLeaflet({
    leaflet() %>%
      addProviderTiles("Esri.WorldImagery", group = "ESRI World Imagery") %>%
      addCircleMarkers(
        data = ships,
        ~ long ,
        ~ lat,
        popup =  ~ speed,
        radius = 5 ,
        stroke = FALSE,
        fillOpacity = 0.8,
        popupOptions = popupOptions(closeButton = FALSE)
      )
  })

  data_map <- reactive({
    if (is.null(input$leafletmap_bounds)) {
      ships
    } else {
      bounds <- input$leafletmap_bounds
      in_bounding_box(ships, lat, long, bounds, speed)
    }
  })

  output$tbl <- DT::renderDataTable({
    DT::datatable(
      data_map(),
      extensions = "Scroller",
      style = "bootstrap",
      class = "compact",
      width = "100%",
      options = list(
        deferRender = TRUE,
        scrollY = 300,
        scroller = TRUE,
        dom = 'tp'
      )
    )
  })


}

shinyApp(ui = ui, server = server)

更新

进行以下更改data = data_map()似乎有效,但有例外:

  output$leafletmap <- renderLeaflet({
    leaflet() %>%
      addProviderTiles("Esri.WorldImagery", group = "ESRI World Imagery") %>%
      addCircleMarkers(
        data = data_map(), #### THIS LINE HAS CHANGED
        ~ long ,
        ~ lat,
        popup =  ~ speed,
        radius = 5 ,
        stroke = FALSE,
        fillOpacity = 0.8,
        popupOptions = popupOptions(closeButton = FALSE)
      )
  })

但是,传单地图不允许我缩小滤波点定义的区域。有办法解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

如果您为地图数据定义了一个被动反应并在renderLeaflet内使用它,那么它应该允许您移出已定义的地图数据。您不需要更改任何其他功能或被动功能,只需添加新的被动功能并对renderLeaflet进行一些更改,如下所示

map_data_react <- reactive({

    ships %>% dplyr::filter(speed > input$speed_f)

})


output$leafletmap <- renderLeaflet({

    ships_data <- map_data_react()  # Add this

    ships_data %>% leaflet() %>%
      addProviderTiles("Esri.WorldImagery", group = "ESRI World Imagery") %>%
      addCircleMarkers(
          ~ long ,  # Removed `data = data_map()`
          ~ lat,
          popup =  ~ speed,
          radius = 5 ,
          stroke = FALSE,
          fillOpacity = 0.8,
          popupOptions = popupOptions(closeButton = FALSE)
  )
})