for leaflet for leafletproxy中的多个多边形?

时间:2016-04-23 14:17:14

标签: r shiny leaflet polygon

我正在波士顿对邻居多边形进行中途停留的可视化。这是一个按比例缩小的工作代码:

https://api.linkedin.com/v1/people/~

我想要做的是在leaftletproxy()中创建一个for循环来循环并为波士顿26的每个邻域创建个性化多边形 - 如下所示:

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)

但由于某种原因,leafletProxy()似乎并不喜欢在其中放置for循环 - 是否有更简单的方法来绘制一些不同的多边形,我可以附加不同的弹出窗口,颜色和值?

Bos邻居档案:https://data.cityofboston.gov/City-Services/Boston-Neighborhood-Shapefiles/af56-j7tb

枢纽站:http://bostonopendata.boston.opendata.arcgis.com/datasets/ee7474e2a0aa45cbbdfe0b747a5eb032_4

1 个答案:

答案 0 :(得分:5)

我同意@warmoverflow %>% for(){}不适合%>%使用for。一般来说,我认为在管道中使用for并不是一种很好的做法,但这里的模式可以做你的建议。

library(magrittr)

"test" %>%
{
  for(i in 1:26){
    . <- paste0(.,i)
  }
  return(.)
}

所以在你的例子中,你可以这样做,但我稍后会提出一个“更好”的方法。

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])
            )
    }
    return(.)
  } %>%
  addPopups(longMark, latMark, poppy,
            options = popupOptions(closeButton = FALSE)
  ) %>% ...(and so on)

我认为这是一种更好的处理方法。

leafletProxy("bosmap", data = hubway) %>%
  clearShapes() %>% clearMarkers %>% clearPopups() %>%
  addPolygons(
    data = neighborhoods[1:26,],
    stroke = FALSE,
    color = "red",
    smoothFactor = 0.5,
    fillOpacity = 0.3,
    popup = ~Name
  )