我有同樣的問題,我想我找到了一些工作:描述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")
。