removeShape problem shiny leaflet

290 views
Skip to first unread message

John froeschke

unread,
May 29, 2015, 9:31:02 AM5/29/15
to shiny-...@googlegroups.com
I'm having trouble getting my leaflet app to work correctly in shiny.  Specifically, I would like to be able to turn on/off layers using the check box.  The layers turn on fine but do not turn off correctly (i.e., disappear when unchecking the box).  For a single layer, I can work around this using the clearShapes() function.  But I want to be able to display multiple layers and turn on and off as desired using the removeShape function. I don't know if I'm doing this incorrectly or some other approach is better.  A reproducible example is below


library(leaflet)
library(shiny)
app = shinyApp(
  ui = fluidPage(checkboxInput("blue", "blue", TRUE),
                 checkboxInput("red", "red", FALSE),
                leafletOutput('map')),
  server = function(input, output, session) {
    map = leaflet() %>% addTiles() %>% setView(-119, 34.57, zoom = 7) #%>%
    

    
    #output$map <- renderLeaflet({
    output$map = renderLeaflet(map)
    
############# blue box
    observeEvent
(input$blue, {
     
if(input$blue==TRUE) {
        leafletProxy
("map", session) %>%
          addRectangles
(
            lng1
=-118.456554, lat1=34.078039,
            lng2
=-117.436383, lat2=33.062717,
            fillColor
= "blue")
           
}      
         
})
############# blue box end


############# red box
observeEvent
(input$red, {
 
if(input$red==TRUE) {
    leafletProxy
("map", session) %>%
      addRectangles
(
        lng1
=-119.456554, lat1=35.078039,
        lng2
=-118.436383, lat2=34.062717,
        fillColor
= "red")
 
}
})
 
 
  observeEvent
(input$red, {
   
if(input$red==FALSE) {
      leafletProxy
("map", session) %>%
        clearShapes
() ## This works but removes both boxes which is undesirable
       
#removeShape("red") ## This does not work but want this to removed the red rectangle only
   
}
   
 
})
############# red box end  


 
}
)


app

> sessionInfo()
R version
3.1.3 (2015-03-09)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1


locale
:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    


attached
base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base    


other attached packages
:
[1] shiny_0.12.0         leaflet_0.0.16       rgdal_0.9-2          sp_1.0-17            shinydashboard_0.3.1


loaded via a
namespace (and not attached):
 
[1] digest_0.6.8      grid_3.1.3        htmltools_0.2.6   htmlwidgets_0.4   httpuv_1.3.2      jsonlite_0.9.16  
 
[7] lattice_0.20-30   magrittr_1.5      mime_0.3          R6_2.0.1          Rcpp_0.11.5       rstudio_0.98.1103
[13] rstudioapi_0.3.1  tools_3.1.3       xtable_1.7-4      yaml_2.1.13      


Any help greatly appreciated.

Matt

unread,
May 30, 2015, 3:57:23 AM5/30/15
to shiny-...@googlegroups.com
Hi,

You forgot to put a layerId when you create your rectangle, and you need it to remove it in removeShape.

So you need to have something like this :

############# red box
observeEvent
(input$red, {
 
if(input$red==TRUE) {
    leafletProxy
("map", session) %>%
      addRectangles
(
        lng1
=-119.456554, lat1=35.078039,
 
      lng2=-118.436383, lat2=34.062717,

        layerId
= "red",
Reply all
Reply to author
Forward
0 new messages