Почему addPolylines работают по-разному на карте листовок R Shiny? - PullRequest
0 голосов
/ 30 апреля 2018

У меня есть 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, карта не появится. Код пользовательского интерфейса:

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 голосов
/ 30 апреля 2018

Я смог заставить ваш код работать с двумя очень маленькими настройками:

  • Вы ссылаетесь на 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)
...