clearShapes()不工作 - 用于R的leaflet()

时间:2015-08-20 00:16:56

标签: r shiny leaflet

我无法弄清楚为什么clearshapes()在我的传单闪亮程序中不起作用。我正在尝试删除现有的圈子,并替换为基于我所拥有的输入复选框选择的类别。然而,发生的事情是新圆圈叠加在现有圆圈之上。

以前有人遇到过这个吗?

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)

1 个答案:

答案 0 :(得分:11)

filteredData()为空时看起来有问题。您可以尝试添加if / else:

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

此外,如果您想要所有选定Source的数据点,您可能希望在过滤中使用%in%而不是==

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