簡體   English   中英

如何使用“For”循環來映射多個多邊形與R中閃亮的傳單?

[英]How can I use a “For” loop to map multiple polygons with the leaflet within shiny in R?

我目前正在努力在一個閃亮的應用程序中映射多個多邊形。 閃亮應用程序的目的是獲取一些與疾病傳播有關的數據,並映射風險最高的區域。 應用程序必須能夠點擊“開始!”來映射多個狀態。 按鈕。

(注意:此應用程序非常大(總共6000多行),所以這里只顯示相關代碼,我不想給試圖幫助我的人帶來負擔)

摘錄自:

Server.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)


} 

}

如何讓這段代碼映射多個狀態? 我的傳單電話有什么不對? 我需要這個代碼將多個形狀文件加載到每個形狀文件上的閃亮和繪制多邊形一次並相應地映射它們

我不確定這是否能解決您的問題,但您的示例絕對不可重復,並且還有一些錯誤。 如果要在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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM