4

我无法用传单地图设置这个闪亮的东西。我原来的帖子有两个问题,有人建议我应该开始一个新帖子来解决我的第二个问题:我如何在按速度过滤后让地图显示我的更新数据;无论我更改“速度”还是地图边界,我的表格都会更新,但传单地图不会根据速度过滤器输入更新点。

可重现的代码

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)
      )
  })

但是,传单地图不允许我缩小过滤点定义的区域。有没有解决的办法?

4

1 回答 1

4

如果您仅为地图数据定义一个反应式并在其中使用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)
  )
})
于 2018-05-02T08:29:09.760 回答