我有同样的问题,我想我找到了一些工作:描述here on the Leaflet for R page这里显示在SuperZip example
更改您使用LeafletProxy
作为生成地图的方式。首先,尝试设置您的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
画出这样的线条和圆圈:您需要添加层的ID,以确保
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
})
当您更改参数时,新形状会替换旧形状。这样你就不需要你有的mapOptions(zoomToLimits = "first")
。