r-spatial / mapedit

Interactive editing of spatial data in R
https://www.r-spatial.org/r/2019/03/31/mapedit_leafpm.html
Other
218 stars 33 forks source link

[R-sig-Geo] "Preselecting" shapes on leaflet map shiny using mapedit::selectmod #75

Open tim-salabim opened 6 years ago

tim-salabim commented 6 years ago

The following came up on R-sig-Geo today. I have mirgrated to here as I don't know if @timelyportfolio is on that mailing list:

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 se 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)
timelyportfolio commented 6 years ago

@tim-salabim thanks for posting here. I will demo this. Testing mapedit with new leaflet now.

Fooourche commented 5 years ago

Hi, I'm also interesting to find a way to deselect polygons from the selectMod with a button or other trigger. I don't find a way, but there is surely something Thanks Fabien

eastclintw00d commented 4 years ago

@tim-salabim thanks for posting here. I will demo this. Testing mapedit with new leaflet now.

@timelyportfolio I would also be very interested in a solution to this problem. Did you do a demo for this?

timelyportfolio commented 4 years ago

@gordocabron @Fooourche I'll work on an example to demonstrate/test. If all works, then we can have a discussion about making this easier in mapedit.

cywhale commented 4 years ago

@timelyportfolio HI, I have an option to let user select grid size (e.g., 0.5-degree, 1-degree) for gridded data on shiny. But after changing grid size, all re-drawn grids must be re-selected by user on mapedit map. So I intersect the new grid cells (finer or coarser) with old selected grids and try to make these intersected ones as pre-selected grids on map. I think it's similar problem to this issue. If there any possible solution/example to this issue? Thanks.