-->

在闪亮的过滤单张地图数据(Filtering leaflet map data in shiny)

2019-09-27 17:34发布

我无法建立这种带有光泽的单张地图。 我原来的职位有两个问题,有人建议我应该开始一个新的职位,以解决我的第二个问题: 我如何获得地图,以显示我的更新后的数据我已经通过速度过滤后 ; 我的表得到更新我是否改变“速度”或地图范围,但基于速度滤波器输入单张地图不更新点。

可复制代码

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)

UPDATE

进行以下更改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)
      )
  })

然而,单张地图不让我放大由经过滤波点定义的区域进行。 有没有解决的办法?

Answer 1:

如果你定义的反应只是地图数据和使用中renderLeaflet它应该让你再搬出的定义。 您不需要更改任何其他功能或reactives,只需添加新的反应,并作出一些改动,以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)
  )
})


文章来源: Filtering leaflet map data in shiny