【问题标题】:For loop for multiple polygons in leafletproxy?传单代理中多个多边形的循环?
【发布时间】:2016-08-17 03:41:57
【问题描述】:

我正在用波士顿的邻域多边形对枢纽站进行可视化。这是一个按比例缩小的工作代码:

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() 中创建一个 for 循环,以循环并为波士顿的 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() 似乎不喜欢在其中放置 for 循环——有没有更简单的方法来绘制多个不同的多边形,我可以将不同的弹出窗口、颜色和值附加到?

这里是博斯社区文件:https://data.cityofboston.gov/City-Services/Boston-Neighborhood-Shapefiles/af56-j7tb

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

【问题讨论】:

  • 我怀疑是%&gt;% 操作符不支持里面的for循环?如果是这样,您可以在中间断开链,并使用 for 循环,如果需要,再使用链。

标签: r shiny leaflet polygon


【解决方案1】:

我同意@warmoverflow 的观点,即%&gt;% for(){} 不是将%&gt;%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
  )

【讨论】:

  • 当我尝试以更好的方式做到这一点时,我的所有多边形都尝试相互连接,因此我在德国有多边形连接到美国的多边形。有什么办法解决这个问题吗?
猜你喜欢
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多