我想根据用户的滑动输入范围为形状文件中的人口图着色,但是当我尝试根据用户的输入来对数据进行子集化时,我一直遇到空间多边形数据帧格式错误。从底图中选择表示满足上述条件的200到1000个多边形。DOWNLOAD SHAPE FILE HERE代码:
library(shiny)
library(leaflet)
library(rgdal)
library(RColorBrewer)
#loading LIAs shape file
mp<-readOGR(
dsn="C:/LIAs",
layer="m1",encoding = 'UTF-8')
ui<-fluidPage(
leafletOutput("leaf",height = 600),
# Slider input range
sliderInput(inputId = "pop",
label = "Population Per km2:",
min = 1, max = 155000,
value = c(1,15000)
)
)
server<-function(input,output){
#Base map(default)
output$leaf<-renderLeaflet({
leaflet(mp) %>%
#default map
#Add default OpenStreetMap map tiles
addTiles()%>%
# lias polygons
addPolygons(
data = mp,
fillColor = "blue",
weight = 1, smoothFactor = 0.5,
opacity = 1.0, fillOpacity = 1.0
)
})
#slider input observe event function for population per km2
observeEvent(input$pop, {
pal1 <- colorBin("YlOrRd", mp$PpDnsty, bins=15, na.color = "#bdbdbd")
leafletProxy("leaf") %>%
# clearMarkers() %>%
clearControls() %>%
clearShapes()%>%
addPolygons(
data = mp[mp$PpDnsty == input$pop, ],
weight = 1, smoothFactor = 0.5,
opacity = 1.0, fillOpacity = 1.0,
fillColor = ~pal1(PpDnsty)
)%>%
addLegend(title = "Population Per km2", position = "topleft",
pal = pal1, values = ~PpDnsty, opacity = 1)
})
}
shinyApp(ui,server)