2015-10-14 55 views
0

下面的代碼是我寫作的閃亮應用的一個精簡版本。問題在於,在複選框列表中取消選中各個選項時,標記不會消失。在這個例子中,我考慮了herehere的解決方案,但都沒有成功,所以我想我一定會錯過一些東西。任何幫助深表感謝!基於閃亮複選框的傳單中清晰的形狀GroupInput

#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

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