"Preselecting" shapes on leaflet map shiny using mapedit::selectmod

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

"Preselecting" shapes on leaflet map shiny using mapedit::selectmod

Nevil Amos
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)

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: "Preselecting" shapes on leaflet map shiny using mapedit::selectmod

Kent Johnson
On Thu, Mar 22, 2018 at 7:00 AM, <[hidden email]> wrote:

>
> Message: 1
> Date: Thu, 22 Mar 2018 12:37:00 +1100
> From: nevil amos <[hidden email]>
> To: [hidden email]
> Subject: [R-sig-Geo] "Preselecting" shapes on leaflet map shiny using
>         mapedit::selectmod
>
> 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 did something very similar to this using just leaflet and shiny (no
mapedit). I was drawing many polygons and wanted to change the state of a
polygon based on a click.

I don't have the code handy, but basically it was
- create an initial map drawing the polygons with the desired initial
state. Use a unique layerId for each polygon.
- listen to input$map_shape_click with observeEvent. On click, remove the
clicked polygon from the map with leaflet::removeShape(leafletProxy('map',
input$map_shape_click$layerId), change the state of the polygon, and add it
back to the map.
 - Save the state when a "Save" button is clicked.

If there are polygons you don't want to respond to clicks, you will have to
filter them out in the click handler using layerId.

See the Inputs/Events section of the leaflet docs here for more
information: https://rstudio.github.io/leaflet/shiny.html

HTH,
kent

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: "Preselecting" shapes on leaflet map shiny using mapedit::selectmod

Kent Johnson
Here is a minimal example:

https://gist.github.com/kent37/63c55c0bbdb0640d8c43369cdeeebdd2



Kent

On Thu, Mar 22, 2018 at 9:11 AM, Kent Johnson <[hidden email]> wrote:

> On Thu, Mar 22, 2018 at 7:00 AM, <[hidden email]> wrote:
>>
>> Message: 1
>> Date: Thu, 22 Mar 2018 12:37:00 +1100
>> From: nevil amos <[hidden email]>
>> To: [hidden email]
>> Subject: [R-sig-Geo] "Preselecting" shapes on leaflet map shiny using
>>         mapedit::selectmod
>>
>> 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 did something very similar to this using just leaflet and shiny (no
> mapedit). I was drawing many polygons and wanted to change the state of a
> polygon based on a click.
>
> I don't have the code handy, but basically it was
> - create an initial map drawing the polygons with the desired initial
> state. Use a unique layerId for each polygon.
> - listen to input$map_shape_click with observeEvent. On click, remove the
> clicked polygon from the map with leaflet::removeShape(leafletProxy('map',
> input$map_shape_click$layerId), change the state of the polygon, and add
> it back to the map.
>  - Save the state when a "Save" button is clicked.
>
> If there are polygons you don't want to respond to clicks, you will have
> to filter them out in the click handler using layerId.
>
> See the Inputs/Events section of the leaflet docs here for more
> information: https://rstudio.github.io/leaflet/shiny.html
>
> HTH,
> kent
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Fwd: "Preselecting" shapes on leaflet map shiny using mapedit::selectmod

Nevil Amos
---------- Forwarded message ----------
From: nevil amos <[hidden email]>
Date: 25 March 2018 at 23:12
Subject: Re: "Preselecting" shapes on leaflet map shiny using
mapedit::selectmod
To: Kent Johnson <[hidden email]>


Tahnks,

I have adapted your code a litle to get further towards what I need., could
not work out how to respond on the git gist site so sending to you direct.

I have now set up the app so that it initially shows the preselected
polygons, but does not allow clicking to change the selection.    changing
the radio button "turns on" the selection,  but when I change the button
back to no it does not trun it off again.  how do I trun of the
map_shape_click again?

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)

On 23 March 2018 at 06:44, Kent Johnson <[hidden email]> wrote:

> Here is a minimal example:
>
> https://gist.github.com/kent37/63c55c0bbdb0640d8c43369cdeeebdd2
>
>
>
> Kent
>
> On Thu, Mar 22, 2018 at 9:11 AM, Kent Johnson <[hidden email]> wrote:
>
>> On Thu, Mar 22, 2018 at 7:00 AM, <[hidden email]> wrote:
>>>
>>> Message: 1
>>> Date: Thu, 22 Mar 2018 12:37:00 +1100
>>> From: nevil amos <[hidden email]>
>>> To: [hidden email]
>>> Subject: [R-sig-Geo] "Preselecting" shapes on leaflet map shiny using
>>>         mapedit::selectmod
>>>
>>> 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 did something very similar to this using just leaflet and shiny (no
>> mapedit). I was drawing many polygons and wanted to change the state of a
>> polygon based on a click.
>>
>> I don't have the code handy, but basically it was
>> - create an initial map drawing the polygons with the desired initial
>> state. Use a unique layerId for each polygon.
>> - listen to input$map_shape_click with observeEvent. On click, remove the
>> clicked polygon from the map with leaflet::removeShape(leafletProxy('map',
>> input$map_shape_click$layerId), change the state of the polygon, and add
>> it back to the map.
>>  - Save the state when a "Save" button is clicked.
>>
>> If there are polygons you don't want to respond to clicks, you will have
>> to filter them out in the click handler using layerId.
>>
>> See the Inputs/Events section of the leaflet docs here for more
>> information: https://rstudio.github.io/leaflet/shiny.html
>>
>> HTH,
>> kent
>>
>
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: "Preselecting" shapes on leaflet map shiny using mapedit::selectmod

Kent Johnson
In reply to this post by Nevil Amos
>
> Date: Sun, 25 Mar 2018 23:14:36 +1100
> From: nevil amos <[hidden email]>
> To: [hidden email]
> Subject: [R-sig-Geo] Fwd: "Preselecting" shapes on leaflet map shiny
>         using mapedit::selectmod
>
> Tahnks,
>
> I have adapted your code a litle to get further towards what I need., could
> not work out how to respond on the git gist site so sending to you direct.
>
> I have now set up the app so that it initially shows the preselected
> polygons, but does not allow clicking to change the selection.    changing
> the radio button "turns on" the selection,  but when I change the button
> back to no it does not trun it off again.  how do I trun of the
> map_shape_click again?
>

Remove the observe() and put the check for input$selectable inside
observeEvent(). If the input is not selectable then you ignore the click
event; if it is selectable then you handle the click. You don't need to
respond directly to changes in input$selectable.


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

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo