为什么addPolylines在R Shiny传单地图上的工作方式不同?

时间:2018-04-30 13:14:18

标签: r shiny leaflet r-leaflet

我有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

这正是我想要的,它看起来像这样:

enter image description here

然而,当我把它放入一个R闪亮的应用程序时,地图将不会出现。 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")

有谁知道为什么会发生这种情况以及我可以采取哪些措施来解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:0)

我能够通过两个非常小的调整来使用您的代码:

  • 您在myMap函数中引用renderLeaflet但尚未定义,因此我将第一行修改为myMap <- leaflet() %>%
  • 您不会从renderLeaflet函数返回任何内容,因此我在myMap之后添加了for-loop语句。

工作代码如下所示,希望这有帮助!

enter image description here

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)