thanks again for your help
# Draw a map which responds to clicks by toggling the selection state
# of the clicked polygon.
library(shiny)
library(leaflet)
library(sf)
nc=st_as_sf(gadmCHE)
nc$selected<-0
# Initial selection
nc$selected[nc$NAME_1%in%c("Aargau", "Appenzell Ausserrhoden", "Appenzell
Innerrhoden",
"Basel-Landschaft", "Basel-Stadt", "Bern",
"Fribourg")]<-1
ui <- fluidPage(
titlePanel("Leaflet selection demo"),
radioButtons(inputId = "selectable",label = "allow selection by
clicking?",choiceNames = c("no","yes"),choices = c("no","yes")),
leafletOutput('map')
)
server <- function(input, output) {
output$map = renderLeaflet({
leaflet() %>% addTiles() %>%
addPolygons(data=nc, fillColor=~c('red', 'blue')[selected+1],
layerId=~NAME_1,
label=~NAME_1)
})
observe({
if (input$selectable=="yes"){
observeEvent(input$map_shape_click, ignoreInit=TRUE, {
id = input$map_shape_click$id
map = leafletProxy('map') %>%
removeShape(id)
nc_local = nc
ix = which(nc_local$NAME_1==id)
nc_local$selected[ix] = 1 - nc_local$selected[ix]
nc <<- nc_local
map %>% addPolygons(data=nc_local[ix,], fillColor=~c('red',
'blue')[selected+1],
layerId=id)
})
}else{
#make the map not selectable
}
}
)
xx<<-nc[nc$selected==1]
}
shinyApp(ui = ui, server = server)