我的RShiny应用程序出现问题,无法过滤Leaflet地图上的标记。我究竟做错了什么?我无法确定为什么会这样。我做了同样的事情,但是以其他方式,例如,使用基于input $ df的开关来切换传单代理中的数据集。
这是我的完整代码
UI:
ui <- fluidPage(
theme = shinytheme("superhero"),
tags$head(
includeCSS("MarkerCluster.Default.css", "MarkerCluster.css"),
includeScript("leaflet.markercluster-src.js"),
),
titlePanel("Map App"),
sidebarLayout(
position = "right",
sidebarPanel(
h3("Options"),
selectInput(
"df",
h5("Display facilities"),
choices = list(
"All" = 3,
"Empty" = 2,
"Non-empty" = 1
),
selected = 3
),
),
mainPanel(
h3("Map demo with MarkerClusters"),
tabsetPanel(
type = "tabs",
tabPanel(
"Map",
leafletOutput("map1", width = "100%", height = "764px"),
),
tabPanel("Data", h4("Showing first 100 rows"), tableOutput("data"))
)
)
)
)
服务器:
server <- function(input, output) {
output$map1 <- renderLeaflet({
leaflet() %>%
addTiles(attribution = "Map Demo") %>%
setView(-98.5795, 39.828175, zoom = 3)
})
output$data <- renderTable({
ds_comp2[1:100, ]
})
observe({
filter <- reactive({
switch(input$df,
"1" = ds_comp2[ds_comp2$empty == F,],
"2" = ds_comp2[ds_comp2$empty == T,],
"3" = ds_comp2[,]
)})
proxy <- leafletProxy("map1") %>%
clearMarkerClusters() %>%
clearMarkers() %>%
addMarkers(
clusterOptions = markerClusterOptions(),
data = filter(),
popup = paste(
"<b>ZIP code:</b>",
ds_comp2$zip,
"<br>",
"<b>Type:</b>",
ds_comp2$type,
"<br>",
"<b>Group:</b>",
ds_comp2$group,
"<br>",
"<b>Empty?:</b>",!(ds_comp2$empty),
"<br>"
)
)
})
}
shinyApp(ui = ui, server = server)
编辑:
数据头
type group empty zip lon lat
1 1 3 TRUE 01913 -70.94279 42.85258
2 0 3 TRUE 92708 -117.96005 33.71688
3 1 3 FALSE 97402 -123.22592 44.04315
4 0 3 TRUE 02109 -71.04829 42.36606
5 0 1 FALSE 92626 -117.90732 33.68341
6 0 2 TRUE 94103 -122.40992 37.77264
7 1 2 TRUE 21801 -75.63245 38.40015
8 0 2 TRUE 10011 -74.00945 40.74650
9 1 2 FALSE 78701 -97.74439 30.27292
10 1 2 FALSE 99019 -117.06447 47.63483
答案 0 :(得分:0)
这似乎对我有用。 leafletProxy
中将单独拥有observe
,并且filter
是一个单独的reactive
块。似乎地图标记是基于过滤后的数据显示的。让我知道这是否适合您。
server <- function(input, output) {
output$map1 <- renderLeaflet({
leaflet() %>%
addTiles(attribution = "Map Demo") %>%
setView(-98.5795, 39.828175, zoom = 3)
})
output$data <- renderTable({
ds_comp2[1:100, ]
})
filter <- reactive({
switch(input$df,
"1" = ds_comp2[ds_comp2$empty == F,],
"2" = ds_comp2[ds_comp2$empty == T,],
"3" = ds_comp2[,]
)
})
observe({
proxy <- leafletProxy("map1") %>%
clearMarkerClusters() %>%
clearMarkers() %>%
addMarkers(
clusterOptions = markerClusterOptions(),
data = filter(),
popup = paste(
"<b>ZIP code:</b>",
ds_comp2$zip,
"<br>",
"<b>Type:</b>",
ds_comp2$type,
"<br>",
"<b>Group:</b>",
ds_comp2$group,
"<br>",
"<b>Empty?:</b>",!(ds_comp2$empty),
"<br>"
)
)
})
}