为什么 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() %>%
  • 你没有return来自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)