[R-sig-Geo] "Preselecting" shapes on leaflet map shiny using mapedit::selectmod
Kent Johnson
kent3737 at gmail.com
Mon Mar 26 14:26:09 CEST 2018
>
> Date: Sun, 25 Mar 2018 23:14:36 +1100
> From: nevil amos <nevil.amos at gmail.com>
> To: r-sig-geo at r-project.org
> 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]]
More information about the R-sig-Geo
mailing list