簡體   English   中英

為什么addPolylines在R Shiny單張地圖上的工作方式不同?

[英]Why does addPolylines work differently on an R Shiny leaflet map?

我有R代碼,該代碼創建了一個由addPolylines()連接的點的傳單地圖。

library(shiny)
library(leaflet)

station = c("A", "B", "C", "D", "E", "F")
latitude = c(-1.63, -1.62, -1.62, -1.77, -1.85, -1.85)
longitude = c(34.3, 34.4, 34.7, 34.3, 34.5, 34.7)
big = c(0, 20, 60, 90, 50, 10)
small = c(100, 80, 40, 10, 50, 90)
colour = c("blue", "blue", "red", "red", "black", "black")
group = c("A", "A", "B", "B", "C", "C")

df = cbind.data.frame(station, latitude, longitude, big, small, colour, group)

colnames(df) = c("station", "latitude", "longitude", "big", "small", "colour", "group")



myMap = leaflet() %>%
    setView(lng = 34.4, lat = -1.653, zoom = 8) %>%
    addTiles()%>%

  addCircles(data = df,
             lng = ~ longitude, lat = ~ latitude,
             color = ~ colour,
             radius = 2000,
             stroke = TRUE,
             opacity = 5,
             weight = 1,
             fillColor = ~ colour,
             fillOpacity = 1)

for(group in levels(df$group)){
  myMap = addPolylines(myMap, 
                      lng= ~ longitude,
                      lat= ~ latitude,
                      data = df[df$group == group,], 
                      color= ~ colour,
                      weight = 3)
}

myMap

這正是我想要的,它看起來像這樣:

在此處輸入圖片說明

但是,當我將其放入R Shiny應用程序中時,地圖將不會出現。 ui代碼為:

fluidPage(

  theme = shinythemes::shinytheme("yeti"),

  titlePanel(title = "Polyline Map"))

    mainPanel("",
              helpText("This is the polyline map"),
              hr(),
              leafletOutput("myMap", height = 400, width = 600)

    )

服務器代碼為:

function(input, output, session) {

  output$myMap = renderLeaflet({
    leaflet() %>%
      setView(lng = 34.4, lat = -1.653, zoom = 8) %>%
  addTiles()%>%

  addCircles(data = df,
               lng = ~ longitude, lat = ~ latitude,
               color = ~ colour,
               radius = 4000,
               stroke = TRUE, 
               opacity = 5,
               weight = 1,
               fillColor = ~ colour,
               fillOpacity = 0.5)

    for(group in levels(df$group)){
      myMap = addPolylines(myMap,
                           lng= ~ longitude,
                           lat= ~ latitude,
                           data = df[df$group==group,],
                           color= ~ colour,
                           weight = 3)
    }
  }

  )}

全局代碼是:

library(shiny)
library(leaflet)

station = c("A", "B", "C", "D", "E", "F")
latitude = c(-1.63, -1.62, -1.62, -1.77, -1.85, -1.85)
longitude = c(34.3, 34.4, 34.7, 34.3, 34.5, 34.7)
big = c(0, 20, 60, 90, 50, 10)
small = c(100, 80, 40, 10, 50, 90)
colour = c("blue", "blue", "red", "red", "black", "black")
group = c("A", "A", "B", "B", "C", "C")

df = cbind.data.frame(station, latitude, longitude, big, small, colour, group)

colnames(df) = c("station", "latitude", "longitude", "big", "small", "colour", "group")

有誰知道為什么會發生這種情況以及我該如何解決? 謝謝!

通過兩個很小的調整,我就能使您的代碼工作:

  • 您在renderLeaflet函數中引用了myMap ,但尚未定義,因此我將第一行修改為myMap <- leaflet() %>%
  • 您不會從renderLeaflet函數返回任何內容,因此我在for-loop之后添加了語句myMap

工作代碼如下所示,希望對您有所幫助!


在此處輸入圖片說明


library(shiny)
library(leaflet)

station = c("A", "B", "C", "D", "E", "F")
latitude = c(-1.63, -1.62, -1.62, -1.77, -1.85, -1.85)
longitude = c(34.3, 34.4, 34.7, 34.3, 34.5, 34.7)
big = c(0, 20, 60, 90, 50, 10)
small = c(100, 80, 40, 10, 50, 90)
colour = c("blue", "blue", "red", "red", "black", "black")
group = c("A", "A", "B", "B", "C", "C")

df = cbind.data.frame(station, latitude, longitude, big, small, colour, group)
colnames(df) = c("station", "latitude", "longitude", "big", "small", "colour", "group")

ui <- fluidPage(
  theme = shinythemes::shinytheme("yeti"),
  titlePanel(title = "Polyline Map"),
  mainPanel("",
            helpText("This is the polyline map"),
            hr(),
            leafletOutput("myMap", height = 400, width = 600)
  )
)

server <- function(input, output, session) {

  output$myMap = renderLeaflet({
    myMap <- leaflet() %>%
      setView(lng = 34.4, lat = -1.653, zoom = 8) %>%
      addTiles()%>%

      addCircles(data = df,
                 lng = ~ longitude, lat = ~ latitude,
                 color = ~ colour,
                 radius = 4000,
                 stroke = TRUE, 
                 opacity = 5,
                 weight = 1,
                 fillColor = ~ colour,
                 fillOpacity = 0.5)

    for(group in levels(df$group)){
      myMap = addPolylines(myMap,
                           lng= ~ longitude,
                           lat= ~ latitude,
                           data = df[df$group==group,],
                           color= ~ colour,
                           weight = 3)
    }
    myMap
  })

  }

shinyApp(ui,server)

暫無
暫無

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

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