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

时间:2015-10-14 18:16:03

标签: r leaflet interactive

下面的代码是我正在编写的闪亮应用的精简版本。问题是当复选框列表中未选中相应选项时,标记不会消失。在示例中,我考虑过来自herehere的解决方案,但没有一个工作,所以我想我必须遗漏一些东西。非常感谢任何帮助!

#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)

1 个答案:

答案 0 :(得分:2)

您好,您需要使用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)
  }
})