Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

shiny - clearShapes() not working -- leaflet() for R

I cannot figure out why clearshapes() is not working in my leaflet shiny program. I am trying to remove the existing circles and replace with a category that is selected based on the input check box that I have. However, what happens is that new circles are overlayed on top of the existing ones.

Anyone encounter this before?

df = read.csv("mappingData.csv",header=T, sep =",")

ui = fluidPage(
  checkboxGroupInput("set", label = "Pothole Reported by:",
                     choices = list("Citizens Connect App" = "Citizens Connect App", 
                                    "City Worker App" = "City Worker App",
                                    "Constituent Call" = "Constituent Call",
                                    "Self Service" = "Self Service",
                                    "Employee Generated" = "Employee Generated",
                                    "Not Available (Cambridge)" = "")),

  verbatimTextOutput("value"),

  leafletOutput("map")
)


server <- function(input, output) {

  filteredDataCheck <- reactive({
    # subset(df, Source == input$set)
    print(input$set)
  })

  output$value <- renderPrint ({ filteredDataCheck() })

  filteredData <- reactive({
    df[as.character(df$Source) == input$set, ]
  })


  output$map <- renderLeaflet ({
    leaflet(df) %>%
      setView(-71.083, 42.353, 13) %>%
     addProviderTiles("Stamen.TonerLite", options = providerTileOptions(noWrap=T))
  })

  observe({
    leafletProxy("map", data = filteredData() ) %>% clearShapes() %>%
      addCircles(radius = 1, color = "red", group = "circles") %>% clearShapes()

  })

}

shinyApp(ui = ui, server = server)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like there is an issue when filteredData() is empty. You can trying adding an if/else:

if(nrow(filteredData())==0) { leafletProxy("map") %>% clearShapes()} 
else {
    leafletProxy("map", data = filteredData() ) %>% clearShapes() %>%
      addCircles(radius = 1, color = "red", group = "circles")
    }

Also if you want all the data points that have a selected Source, you might want to use %in% instead of == in your filtering:

df[as.character(df$Source) %in% input$set, ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...