Hi I am putting together a shiny map app in which I want to allow for selection or deselection of polygons from a map layer.
Importantly At the beginning of the process I would like a subset of the polygons to be already selected ( and they can then be deselected using a click if required).
The final set of selected polygons is then saved to disk.
Ideally I would like to be able ( eg using a shiny action button) to reset the selection to the pre-selected polygons.
I have the basic click select working fine, but cannot work out how to pre-select some polygons so that they are dis as selected ( and can be deselected with a click) at the start of the process.
Secondly is there a way to reset the selection to the pre-selected set.
The pre-selected polygon ids will change depending on another interactive.
toy example of code is below with comments about the two issues that I cannot figure out:
library(shiny)
library(mapedit)
library(leaflet)
outputDir<-getwd()
saveData <- function(data,fileName) {
#Write the file to the local system
write.csv(
x = data,
file = file.path(outputDir, fileName),
row.names = FALSE, quote = TRUE
)
}
# An example of preslected values for the polygon layer
# in the real app this will change based on other selections in the UI
preselected1<-gadmCHE$NAME_1[1:3]
preselected2<-gadmCHE$NAME_1[5:8]
ui <- fluidPage(
radioButtons(inputId = "presel",label = "Preselection",choiceNames = c("preselected1","preselected2"),choices = c("preselected1","preselected2")),
actionButton("startSel", "Show Polygons"),
actionButton("endSel", "Do not Save"),
actionButton("saveSel","Save Revised Selection"),
actionButton("resetSel","Reset Selection\n does not work at the moment"),
selectModUI("selector")
)
server <- function(input, output) {
ns <<- NS("selector")
base_map <- leaflet() %>%
addTiles()%>%
addPolygons(data =gadmCHE,
label = ~NAME_1,
layerId = ~NAME_1,
group="Polygons")
####HOW can I feed in the preslected ids to the process so they
####are displayed when the polygons group is shown ?
Selects<<-callModule(selectMod,
"selector",
base_map,
styleFalse = list(fillOpacity =0,
weight = 1,
opacity = 1),
styleTrue = list(fillOpacity = 1,
weight= 1,
opacity = 0))
leafletProxy(ns("map"))%>%
hideGroup("Polygons")
observe({selOuts<-Selects()
selOuts<<-selOuts
selOut<<-t(selOuts[selOuts$selected==TRUE,"id"])})
observeEvent(input$startSel,{
#HOW can I feed in the preslected ids to the process so they
#are displayed when the polygons group is shown ?
leafletProxy(ns("map"))%>%
showGroup("Polygons")})
observeEvent(input$resetSel,{
#How can I reset the polygons to the preselected set?
leafletProxy(ns("map"))%>%
showGroup("Polygons")})
observeEvent(input$endSel,{
leafletProxy(ns("map"))%>%
hideGroup("Polygons")})
observeEvent(input$saveSel,{
outname<-"Revision.csv"
saveData(selOut,outname)
rm(outname)
})
}
shinyApp(ui = ui, server = server)