我希望能够在传单中更新绘制多边形的属性,而无需再次绘制多边形。这使得它相当慢,因为我想将它用于时间序列,所以它们需要快速更新。我添加了一些示例代码,其中按钮更新颜色(多边形),但由于重绘,它很慢。任何建议都会受到欢迎!我重用了以前问题的一些元素,以获得一个下载自己的shapefile的工作示例。期待您的解决方案!
# bits of code from: https://stackoverflow.com/questions/29118059/display-spatialpolygonsdataframe-on-leaflet-map-with-r
library(rgdal)
library(leaflet)
library(shiny)
download.file(file.path('http://www.naturalearthdata.com/http/',
'www.naturalearthdata.com/download/50m/cultural',
'ne_50m_admin_0_countries.zip'),
f <- tempfile())
unzip(f, exdir=tempdir())
world <- readOGR(tempdir(), 'ne_50m_admin_0_countries', encoding='UTF-8')
#lets grab 20 countries:
commonwealth <- c("Antigua and Barb.", "Australia", "Bahamas", "Bangladesh",
"Barbados", "Belize", "Botswana", "Brunei", "Cameroon", "Canada", "Cyprus",
"Dominica", "Fiji", "Ghana", "Grenada", "Guyana", "India", "Jamaica", "Kenya",
"Kiribati")
col<-c("red","green", "yellow","blue")
ui <- fluidPage(
leafletOutput("mymap"),
p(),
actionButton("recolor", "New colors")
)
server <- function(input, output, session) {
points <- eventReactive(input$recolor, {
sample(col, 20, replace=TRUE)
}, ignoreNULL = FALSE)
output$mymap <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$Stamen.TonerLite,
options = providerTileOptions(noWrap = TRUE)
) %>%
addPolygons(data=subset(world, NAME %in% commonwealth), weight=2, color=~sample(col, 20, replace=TRUE))
})
observe({
leafletProxy("mymap", data = points()) %>%
clearShapes() %>%
addPolygons(data=subset(world, NAME %in% commonwealth), weight=2, color=~points())
})
}
shinyApp(ui, server)