layering in leaflet in a shiny app

228 views
Skip to first unread message

ArjunaCap

unread,
Jun 2, 2015, 5:03:36 PM6/2/15
to shiny-...@googlegroups.com
In the MRE below, I want to plot the triangle (from addPolygons) beneath the circles (from addCircleMarkers) in order to use a popup when the user clicks a circle.  

In the standalone plot, the ordering of arguments accomplishes this:

    leaflet() %>% addTiles() %>% addPolygons() %>% addCircleMarkers()

The Problem
I've additionally added the ability to toggle the shape via leafletProxy, but doing so always puts the polygon over the drawn circles, even if I order the plotting as above.

How can I ensure that the circles are always drawn on top?

Any help is appreciated.


The MRE:
library(shiny)

stuttgart
<- data.frame(lat = c(49.340052, 49.400860, 48.029136), lng = c(8.011806, 10.923183,  9.195583))

ui
<- fluidPage(
  leafletOutput
("mymap"),
  p
(),
  checkboxInput
("show", "Show Stuttgart Triangle", TRUE))

server
<- function(input, output, session) {
 
  points
<- eventReactive(input$recalc, {
    cbind
(rnorm(20) + 9.2, rnorm(20) + 48.79)
 
}, ignoreNULL = FALSE)
 
 
### Base plot
  output$mymap
<- renderLeaflet({
    leaflet
() %>%
      addTiles
()
      addPolygons
(lat = stuttgart$lat, lng = stuttgart$lng, color = "#FFBF00", layerId = "stuttgart") %>%
      addCircleMarkers
(data = points(), popup = "Here!")
 
})

 
### Takes away the polygon
  observeEvent
(input$show, {
   
if(input$show == FALSE) {
      leafletProxy
("mymap", session) %>%
       removeShape
(
          layerId
= "stuttgart")
   
}
 
})


 
### Puts the polygon  back
 
  observeEvent
(input$show, {
   
if(input$show == TRUE) {
      leafletProxy
("mymap", session) %>%
        addPolygons
(lat = stuttgart$lat, lng = stuttgart$lng, color = "#FFBF00", layerId = "stuttgart")
     
%>%     addCircleMarkers(data = points(), popup = "Here!")
   
}
 
})
}

shinyApp
(ui, server)

Joe Cheng

unread,
Jun 2, 2015, 5:46:07 PM6/2/15
to ArjunaCap, shiny-...@googlegroups.com
This isn't currently possible, sorry. However, I'm currently working on supporting Layers Control which will give you a much more direct way to express this.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/7562e10e-b1c7-41c0-8a0d-936407201358%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages