基于闪亮的checkboxGroupInput清除传单中的形状

基于闪亮的checkboxGroupInput清除传单中的形状,r,leaflet,interactive,R,Leaflet,Interactive,下面的代码是我正在编写的闪亮应用程序的精简版本。问题是,如果在复选框列表中取消选中相应的选项,标记将不会消失。在这个例子中,我考虑了来自和的解决方案,但都不起作用,所以我想我肯定遗漏了一些东西。非常感谢您的帮助 #Read source data mydat <- data.table(entitynum=c(400, 201, 602, 304), londd=c(20, 38, 96, 32), latdd

下面的代码是我正在编写的闪亮应用程序的精简版本。问题是,如果在复选框列表中取消选中相应的选项,标记将不会消失。在这个例子中,我考虑了来自和的解决方案,但都不起作用,所以我想我肯定遗漏了一些东西。非常感谢您的帮助

#Read source data
mydat <- data.table(entitynum=c(400, 201, 602, 304),
                    londd=c(20, 38, 96, 32),
                    latdd=c(60, 56, 30, 31),
                    flag=c("karoo", "water", "saida", "nina"))

#Set up ui
ui <- shinyUI(fluidPage(title="",

  #App title
  titlePanel(h3("My tool", align="left")),

  #App layout
  sidebarLayout(position="left",

    #App sidePanel content and styles
    sidebarPanel(h5("", width=2),
                 checkboxGroupInput(inputId="InFlags", label=h4("Flag"), 
                                    choices=setNames(object=c("karoo", "water", "saida", "nina"),
                                                     nm=c("karoo", "water", "saida", "nina"))),
                 position="left"),

    #App mainPanel content and styles
    mainPanel(fluidRow(leafletOutput(outputId="lmap")))

  )
)
)

#Set up server
server <- function(input, output){
#Build leaflet map
lmap <- leaflet(data=mydat)%>%
            addProviderTiles(provider="MapQuestOpen.OSM")%>%
          fitBounds(~min(londd), ~min(latdd), ~max(londd), ~max(latdd))

#Filter data
datFilt <- reactive(mydat[flag%in%input$InFlags])

#Add markers based on selected flags
observe({

if(nrow(datFilt())==0) {print("Nothing selected");leafletProxy("lmap") %>% clearShapes()}
  else{

  print(paste0("Selected: ", unique(input$InFlags)))

  leafletProxy("lmap", data=datFilt())%>%clearShapes()%>%
  addCircleMarkers(lng=~londd, lat=~latdd,
                   clusterOptions=markerClusterOptions(), weight=3,
                   color="#33CC33", opacity=1, fillColor="#FF9900", 
                   fillOpacity=0.8)%>% clearShapes()
}

})

output$lmap <- renderLeaflet(lmap)
}

#Run app
shinyApp(ui = ui, server = server)
#读取源数据
mydat%
addCircleMarkers(液化天然气=~londd,纬度=~latdd,
clusterOptions=markerClusterOptions(),权重=3,
color=“#33CC33”,不透明度=1,fillColor=“#FF9900”,
fillOpacity=0.8)%>%clearShapes()
}
})

输出$lmapHi您需要使用
clearMarkerClusters
而不是
clearShapes
,例如:

observe({
  if(nrow(datFilt())==0) {
    print("Nothing selected")
    leafletProxy("lmap") %>% clearMarkerClusters()
  }
  else{
    print(paste0("Selected: ", unique(input$InFlags)))
    leafletProxy("lmap", data=datFilt()) %>%
      clearMarkerClusters() %>%
      addCircleMarkers(lng=~londd, lat=~latdd,
                       clusterOptions=markerClusterOptions(), weight=3,
                       color="#33CC33", opacity=1, fillColor="#FF9900", 
                       fillOpacity=0.8)
  }
})