4

I'm working with Leaflet maps in Shiny for R. I'm producing a map with some markers on it. When I use addTiles () for my base map, everything works as expected. However with addProviderTiles (), the markers don't appear on the map. Only after I trigger an action that causes them to be redrawn, they show up as I would expect them.

This doesn't seem to be mentioned in the vignettes, so I'm not sure if it's a bug or I'm missing some parameter options.

I made a reproducible example. If you run it as is, the markers will appear normally. If you comment line 13 and uncomment line 14, the markers won't work initially. I added an observer () that redraws the markers when you click on the map.

library(shiny)
library(sp)
library(leaflet)

ui <- bootstrapPage (
                leafletOutput("map", width="80%", height="800px"))

server <- function(input, output, session) {
  output$map <- renderLeaflet({
      print ("render tiles")
      b <- bbox (breweries91)
      leaflet (breweries91) %>%
      addTiles () %>%
#      addProviderTiles (providers$CartoDB.Positron) %>%
      fitBounds (b[1], b[2], b[3], b[4])
      })

  observe ({
      print ("make markers")
      leafletProxy ("map", data = breweries91) %>%
      addAwesomeMarkers ()
      cl <- input$map_click
      if (!is.null (cl))
          print ("click")
      })
}
shinyApp (ui, server)

Edit software versions:

  • Ubuntu: 16.04.2
  • Mozilla Firefox 52.0.1
  • R: 3.3.3
  • leaflet: 1.1.0
  • shiny: 1.0.0
6
  • your code worked fine for me! I commented line 13 and uncommented line 14, I deleted the observer and added the line addAwesomeMarkers() to the server function. I tried runing it in Window and External (RStudio 1.0.136) an everything seems to work fine. I'm using leaflet 1.1.0, shiny 1.0.0, sp 1.2-4.
    – Guz
    Commented Mar 6, 2017 at 12:37
  • The problem is that if I draw the markers inside renderLeaflet, the entire map has to be reloaded if the markers change. That's why they need to be placed in the observer.
    – karpfen
    Commented Mar 7, 2017 at 17:06
  • I don't understand: 1) I made a reproducible example. If you run it as is, the markers will appear normally. That's right! If you comment line 13 and uncomment line 14, the markers won't work initially, doing that the markers work initially in my case!. I don't know how can I help you. I'm using leaflet version 1.1.0 and shiny 1.0.0.
    – Guz
    Commented Mar 23, 2017 at 19:40
  • Interesting, I'm using the same versions, but it doesn't work for me. Maybe the problem lies somewhere else then. I'll try it on some other setup tomorrow. Thanks for the hint!
    – karpfen
    Commented Mar 23, 2017 at 23:29
  • are you using rstudio? Maybe check the browser you are using to watch the map. And enable javascript.
    – Guz
    Commented Mar 24, 2017 at 1:41

1 Answer 1

2
+50

Using the RStudio IDE, your code worked fine in the Viewer Pane. However, it didn't worked using Firefox 52.0.1 browser.

Commenting the observer() function and adding addAwesomeMarkers () to the renderLeaflet({}) worked fine in Firefox 52.0.1 browser. I think Firefox 52.0.1 browser was right, because if you are using an observer function that mean it will draw the markers after the input$map_click action was done and never before that.

One property of the observer function is:

"An observer is like a reactive expression in that it can read reactive values and call reactive expressions ..." (from Shiny package documentation)

Try the suggested code below from your reproducible example:

library(shiny)
library(sp)
library(leaflet)

ui <- bootstrapPage (leafletOutput("map", width = "80%", height = "800px"))

server <- function(input, output, session) {
  output$map <- renderLeaflet({
    print ("render tiles")
    b <- bbox (breweries91)
    leaflet (breweries91) %>%
      # addTiles () %>%
      addProviderTiles (providers$CartoDB.Positron) %>%
      addAwesomeMarkers () %>%
      fitBounds (b[1], b[2], b[3], b[4])
  })

  # observe ({
  #   print ("make markers")
  #   leafletProxy ("map", data = breweries91) %>%
  #     addAwesomeMarkers ()
  #   cl <- input$map_click
  #   if (!is.null (cl))
  #     print ("click")
  # })
}
shinyApp (ui, server)

Also, if you want to preserve the observe function but not to draw the markers at the begining, you can use it in this way:

observe ({
     print ("observe function called")
  #   leafletProxy ("map", data = breweries91) %>%
  #   addAwesomeMarkers ()
     cl <- input$map_click
     if (!is.null (cl))
       print ("click")
   })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.