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

nevil amos nevil.amos at gmail.com
Sun Mar 25 14:14:36 CEST 2018


---------- Forwarded message ----------
From: nevil amos <nevil.amos at gmail.com>
Date: 25 March 2018 at 23:12
Subject: Re: "Preselecting" shapes on leaflet map shiny using
mapedit::selectmod
To: Kent Johnson <kent3737 at gmail.com>


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 <kent3737 at gmail.com> wrote:

> Here is a minimal example:
>
> https://gist.github.com/kent37/63c55c0bbdb0640d8c43369cdeeebdd2
>
>
>
> Kent
>
> On Thu, Mar 22, 2018 at 9:11 AM, Kent Johnson <kent3737 at gmail.com> wrote:
>
>> On Thu, Mar 22, 2018 at 7:00 AM, <r-sig-geo-request at r-project.org> wrote:
>>>
>>> Message: 1
>>> Date: Thu, 22 Mar 2018 12:37:00 +1100
>>> From: nevil amos <nevil.amos at gmail.com>
>>> To: r-sig-geo at r-project.org
>>> 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]]



More information about the R-sig-Geo mailing list