我正在波士顿的邻域多边形中进行中心站的可视化。这里有一个按比例缩小的工作代码:对于leafletproxy中多个多边形的循环?
library(shiny)
library(leaflet)
library(plyr)
library(dplyr)
library(rgdal)
#setwd
setwd("C:/Users/580048/Downloads")
#read hubway station data
hubway <- read.csv("Hubway_Stations.csv")
#read shapefiles
neighborhoods <-readOGR("C:/Users/580048/Downloads/bosneigh/Bos_neighborhoods_new.shp","Bos_neighborhoods_new")
neighborhoods <- spTransform(neighborhoods, CRS("+proj=longlat +datum=WGS84"))
#ui layout
ui <- bootstrapPage(
#style of tags
tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
tags$style(type = "text/css", 'label[for="range"] {color: white;}'),
tags$style(type = "text/css", 'label[for="range2"] {color: white;}'),
tags$style(type = "text/css", 'label[for="team"] {color: white;}'),
tags$style(type = "text/css", 'label[for="away"] {color: white;}'),
tags$style(type = "text/css", 'label {color: white;}'),
#the map
leafletOutput("bosmap", width = "100%", height = "100%")
)
#server functions
server <- function(input, output, session) {
#plot static map
output$bosmap <- renderLeaflet({
leaflet(randomtaxi) %>%
addProviderTiles("CartoDB.DarkMatterNoLabels",
options= providerTileOptions(opacity = 0.99)) %>%
fitBounds(-71.0, 42.3, -71.1, 42.4)
})
#plot filtered cabs
observe({
longMark <- -71.0589
latMark <- 42.3601
poppy <- "Boston"
hotBorough <- subset(neighborhoods, neighborhoods$Name %in% c("Allston"))
totalBorough <- subset(neighborhoods, neighborhoods$Name %in% c("Back Bay"))
leafletProxy("bosmap", data = hubway) %>%
clearShapes() %>% clearMarkers %>% clearPopups() %>%
addPolygons(data = subset(neighborhoods, neighborhoods$Name %in% c(toString(neighborhoods$Name[1]))),
stroke = FALSE,
color = "red",
smoothFactor = 0.5,
fillOpacity = 0.3,
popup = toString(neighborhoods$Name[1])) %>%
addPopups(longMark, latMark, poppy,
options = popupOptions(closeButton = FALSE)
) %>%
addCircles(~hubway$long_,
~hubway$lat,
radius = 200,
weight = 20,
stroke = FALSE, fillOpacity = 0.5)
})
}
shinyApp(ui, server)
我想要做的就是创建一个通过leaftletproxy()循环内循环,并为每一个波士顿的26个社区的各个多边形 - 是这样的:
leafletProxy("bosmap", data = hubway) %>%
clearShapes() %>% clearMarkers %>% clearPopups() %>%
for(i in 1:26){
addPolygons(data = subset(neighborhoods, neighborhoods$Name %in% c(toString(neighborhoods$Name[i]))),
stroke = FALSE,
color = "red",
smoothFactor = 0.5,
fillOpacity = 0.3,
popup = toString(neighborhoods$Name[i])) %>%
}
addPopups(longMark, latMark, poppy,
options = popupOptions(closeButton = FALSE)
) %>% ...(and so on)
但由于某种原因,leafletProxy()似乎不喜欢在其中放置循环 - 是否有更简单的方法绘制大量不同的多边形,以便我可以将不同的弹出窗口,颜色和值附加到?
博斯附近的文件位置:https://data.cityofboston.gov/City-Services/Boston-Neighborhood-Shapefiles/af56-j7tb
hubway站:http://bostonopendata.boston.opendata.arcgis.com/datasets/ee7474e2a0aa45cbbdfe0b747a5eb032_4
我怀疑它是'%>%'运算符不支持循环里面吗?如果是这样,你可以打破中间的链条,并使用循环,然后再次使用链条,如果你需要。 –