gpt4 book ai didi

r - clearShapes() 不起作用——R 的 leaflet()

转载 作者:行者123 更新时间:2023-12-02 02:40:45 26 4
gpt4 key购买 nike

我不明白为什么 clearshapes() 在我的传单 Shiny 程序中不起作用。我正在尝试删除现有的圆圈并替换为根据我拥有的输入复选框选择的类别。然而,发生的情况是新的圆圈叠加在现有的圆圈之上。

有人遇到过这种情况吗?

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)

最佳答案

filteredData() 为空时,似乎存在问题。您可以尝试添加 if/else:

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

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

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

关于r - clearShapes() 不起作用——R 的 leaflet(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32107667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com