Search code examples
rshinyleafletr-leaflet

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


I have R code which creates a leaflet map with points connected by 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

This is exactly what I am wanting and it looks like this:

enter image description here

However, when I put this into an R shiny app, the map will not appear. The ui code is:

fluidPage(

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

  titlePanel(title = "Polyline Map"))

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

    )

The server code is:

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)
    }
  }

  )}

And the global code is:

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")

Does anyone know why this happens and what I can do to fix it? Thank you!


Solution

  • I was able to get your code working with two very small adjustments:

    • You refer to myMap in your renderLeaflet function but that is not defined yet, so I modified the first line to myMap <- leaflet() %>%
    • You do not return anything from the renderLeaflet function, so I added the statement myMap after the for-loop.

    Working code is shown below, hope this helps!


    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)