【发布时间】:2021-03-31 01:32:16
【问题描述】:
是否可以拆分地图的代码,以便地图的一部分仅在其自己的输入发生更改时才更新?
在下面的再现示例中,选择“碳粉”瓦片并选择新站时,再次执行整个传单映射,因为需要更新addLegend 987654321。这使磁贴跳回“OSM(默认)”磁贴。我想留在我选择其他电台时选择的图块。
library(leaflet)
library(shiny)
library(dplyr)
pal <- colorFactor(
palette = "YlGnBu",
domain = quakes$stations
)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
checkboxGroupInput("stations",
"Choose a station",
choices=sort(unique(quakes$stations)),
selected = c(10, 11))
),
mainPanel(
leafletOutput("map")
)
)
)
server <- function(input, output) {
points <- reactive({
quakes %>%
filter(stations %in% input$stations)
})
output$map <- renderLeaflet({
leaflet(quakes) %>%
addTiles(group = "OSM (default)") %>%
addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
addLayersControl(
baseGroups = c("OSM (default)", "Toner"),
options = layersControlOptions(collapsed = FALSE)) %>%
addLegend("Legend", position = "topleft", pal = pal, values = input$stations)
})
observe({
if(nrow(points()) == 0) {
leafletProxy("map", data = points()) %>%
clearMarkers()
} else {
leafletProxy("map", data = points()) %>%
clearMarkers() %>%
addCircleMarkers(radius = 2)
}
})
}
shinyApp(ui, server)
我尝试了几件事,包括在else 语句中添加addLegend,但效果并不好。我是传单/闪亮的新手,移动addLegend 对我来说似乎最合乎逻辑。我非常感谢任何建议!
【问题讨论】:
标签: r shiny leaflet shinydashboard