我有同样的问题,我想我找到了一些有用的东西:
使用以下命令更改生成地图的方式LeafletProxy
如上所述在 R 页面的传单上 http://rstudio.github.io/leaflet/shiny.html并显示在此处超级拉链示例 https://github.com/rstudio/shiny-examples/tree/master/063-superzip-example。首先,尝试设置您的renderLeaflet
像这样的函数:
output$map = renderLeaflet(leaflet() %>%
addTiles(urlTemplate = "http://{s}.tile.thunderforest.com/cycle/{z}/{x}/{y}.png") %>%
setView(...) # add the parameters as appropriate set the view or use fitBounds
然后使用一个observe
功能与LeafletProxy
像这样画直线和圆:
observe({
leafletProxy("map") %>%
clearShapes() %>%
addPolygons(layerId= "layer1"
, data = leeds
, fillOpacity = 0.4
, opacity = (input$transp_zones)*.4
, fillColor = leeds$color_pcycle
) %>%
addPolyLines(layerId = "layer2"
, data = lfast, color = "red"
, opacity = input$transp_fast
, popup = sprintf("<dl><dt>Distance </dt><dd>%s km</dd><dt>Journeys by bike</dt><dd>%s%%</dd>", round(flows$fastest_distance_in_m / 1000, 1), round(flows$p_cycle * 100, 2))
) %>%
# and so on in a similar fashion for the rest of your shapes
})
您需要添加图层 ID,以确保在更改参数时新形状替换旧形状。这样你就不需要mapOptions(zoomToLimits = "first")
你有过。