过滤闪亮的传单地图数据
Filtering leaflet map data in shiny
我在使用传单地图设置这个 shiny 时遇到了问题。我的 有两个问题,有人建议我应该开始一个新的 post 来解决我的第二个问题:如何让地图在我过滤后显示我的更新数据速度;无论我更改 "speed" 还是地图边界,我的 table 都会更新,但传单地图不会根据速度过滤器输入更新点。
可重现代码
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)
)
})
但是,传单地图不允许我缩小过滤点定义的区域。有解决办法吗?
如果您仅为地图数据定义一个反应式并在 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)
)
})
我在使用传单地图设置这个 shiny 时遇到了问题。我的
可重现代码
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)
)
})
但是,传单地图不允许我缩小过滤点定义的区域。有解决办法吗?
如果您仅为地图数据定义一个反应式并在 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)
)
})