clearShapes() 不工作——R 的传单() [英] clearShapes() not working -- leaflet() for R

查看:13
本文介绍了clearShapes() 不工作——R 的传单()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚为什么 clearshapes() 在我的传单闪亮程序中不起作用.我正在尝试删除现有的圆圈并替换为根据我拥有的输入复选框选择的类别.但是,新的圆圈会覆盖在现有圆圈之上.

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)

推荐答案

filteredData() 为空时似乎有问题.您可以尝试添加 if/else:

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")
    }

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

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, ]

这篇关于clearShapes() 不工作——R 的传单()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆