【问题标题】:How can I use a "For" loop to map multiple polygons with the leaflet within shiny in R?如何使用“For”循环将多个多边形与 R 中闪亮的传单映射?
【发布时间】:2016-10-01 09:18:54
【问题描述】:

我目前正在努力在闪亮的应用程序中映射多个多边形。闪亮应用程序的目的是获取一些与在多个州传播的疾病有关的数据,并绘制出风险最高的区域。该应用程序必须能够在单击“开始!”时映射多个状态!按钮。

(注意:这个应用程序非常大(总共 6000 多行),所以这里只显示相关代码,我不想给那些试图帮助我的人增加负担)

摘自:

服务器.R

#The purpose of col_inputs and col_names is to create a two-dimensional array with all of the input parameters for the function. This was done to maintain compatibility with some legacy code. Catted_states on the other hand   combines all states selected into a list. 
(Example: c("AZ","FL","VA")

output$gm <- renderLeaflet({
        global_map(ARG_1, ARG_2, ARG_3)
    })

Global_Map.R

此代码唯一真正的问题是在 for 循环完成后根本没有绘制“M”。

global_map <- function(col_names, col_inputs, catted_states) {


User_para <- array(0, dim = c(16, 2))

for( I in 1:length(states) {
if (state_num > 10) {
read.csv(Loop specific file)
}

 if (state_num < 10) {
read.csv(Loop specific file) 
}
    state_num * Loop specific calculation[I]        


    pal <- colorNumeric(palette = "Purples", domain = state_output$risk)
    pal_sR <- pal(state_output$risk)
    m <- addProviderTiles(m, "CartoDB.Positron")
    m <- addLegend(m, title = "Risk", pal = pal, values = ~state_output$risk, 
        opacity = 0.7)
    m <- addPolygons(m, stroke = FALSE, smoothFactor = 0, fillOpacity = 0.5, 
        color = ~pal_sR)


} 

}

如何获取此代码来映射多个状态?我的传单电话有什么不正确的地方?我需要这段代码将多个形状文件加载到闪亮中并在每个形状文件上绘制一次多边形并相应地映射它们

【问题讨论】:

  • 如果您将示例设为reproducible,则您更有可能得到响应 - 即,将一个最小示例放在一起,以复制您的问题,其他人可以将其复制并粘贴到他们的环境中。跨度>
  • 感谢您的建议。我将相应地更新问题@SymbolixAU

标签: r web mapping shiny leaflet


【解决方案1】:

我不确定这是否能解决您的问题,但您的示例绝对不可重现,并且还有几个错误。如果您想在 for 循环中生成多个多边形,然后将它们添加到传单地图,代码如下:

library(shiny)
library(leaflet)

ui <- fluidPage(
  sliderInput("nPolys", "How many Loops", min = 1, max = 20, value = 3),
  ## Map
  leafletOutput("gm")
)

server <- function(input, output) {

  ## Initialize map
  m = leaflet() %>% addTiles()


  ## Render Map
  output$gm <- renderLeaflet({
    ## Loop
    for (I in 1:input$nPolys) {
      ## Create dummy polygons
      Sr1 = Polygon(cbind(c(2,4,4,1,2)*runif(1,1,10),c(2,3,5,4,2)*runif(1,1,10)))
      Sr2 = Polygon(cbind(c(5,4,2,5)*runif(1,1,10),c(2,3,2,2)*runif(1,1,10)))
      Srs1 = Polygons(list(Sr1), "s1"); Srs2 = Polygons(list(Sr2), "s2")
      SpP = SpatialPolygons(list(Srs1,Srs2), 1:2)

      ## add Polygons to map
      m <- addPolygons(m, data=SpP, stroke = FALSE, smoothFactor = 0, fillOpacity = 0.5) 
    }

    ## Call map !
    m
  })
}

shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 2016-09-17
    • 2015-06-11
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    相关资源
    最近更新 更多