Shiny Leaflet map with Mapedit::selectMap

leaflet

#1

I would like to select polygons from an existing spatial polygon data set interactively in a shiny leaflet map.
The process is straightforward in leaflet/ mapedit, however I need to include the procedure in a shiny app map interface. I cannot work out how to incorporate the selectMap() in the shiny app. An error is returned:

Here is some toy code showing the code in using leaflet locally:

library(mapedit)
library(leaflet)
options(stringsAsFactors = F)
bounds <- c(5.956063, 10.49511, 45.81706, 47.80848 )

lf <- leaflet() %>% 
  addTiles()%>%
  addPolygons(data =gadmCHE,
              label = ~NAME_1,
              layerId = ~NAME_1)

 selected <-selectMap(lf)

##the above works fine and returns a dataframe giving the ids  of the selected polygons.

##however the code below - trying to implement in shiny fails with "Can't call `runApp()` from within `runApp()`. If your application code contains `runApp()`, please remove it.

library(shiny)
library(leaflet)
library(mapedit)


ui <- fluidPage(
      leafletOutput("mymap")
    )

server <- function(input, output, session) {
  
  lf<-leaflet() %>% 
    addTiles()%>%
    addPolygons(data =gadmCHE,
                label = ~NAME_1,
                layerId = ~NAME_1)
  output$mymap <- renderLeaflet({selectMap(lf)
  })
 }
shinyApp(ui, server)

##How do I achieve the same in  a shiny map?

#2

selectMap(lf) will lauch a web service and return a value that you manipulate.

indeed, selectMap() object can not call by renderLeaflet, which should store the value by reactiveValues().

I found this issue will fix your problem: https://github.com/r-spatial/mapedit/issues/69


#3

Thanks for that link.

I have now updated my toy example to work in a shiny map.

library(mapedit)
library(leaflet)

ui <- fluidPage(
  
  selectModUI("eview")
)

server <- function(input, output) {
  
  ns <<- shiny::NS("eview")
  
  lf0 <- leaflet() %>% 
  addTiles()%>%
  addPolygons(data =gadmCHE,
              label = ~NAME_1,
              layerId = ~NAME_1)
  
  callModule(selectMod, "eview", lf0)
}
shinyApp(ui = ui, server = server)

However , without the ability to use renderleaflet or leafeler proxy I cannot turn this toy example into what I actually need to do.

I want to be able to turn on and off overlays of polygons on the map and at the same time swithc on and off the ability to select the the polygons ( through a sidebar radio button for example)

I tried the following but it does not display the polygons or ability to select. can you suggest how I "overlay a selectMod on the leaflet map, use it for selection , and then remove the overlay?

library(mapedit)
library(leaflet)
library(raster)
options(shiny.trace=TRUE)
ui <- fluidPage(

  sidebarLayout(
    sidebarPanel(
    radioButtons(inputId = "SelectionOn",inline = T,choiceNames = c("No","Yes"),choiceValues = c(0,1),label="Do you want to display polygons for selection") 
    ),

  mainPanel(
    selectModUI("eview",height=900)
  )
)
)


server <- function(input, output) {
  
  ns <- NS("eview")
  
  
  lf0 <- leaflet() %>% 
    addTiles()
  
  observe({
    if (input$SelectionOn==1){
      leafletProxy("map")%>%
        addPolygons(data =gadmCHE,
                    label = ~NAME_1,
                    layerId = ~NAME_1,
                    group ="myPolygons")
      callModule(selectMod, "eview", lf0)  
      
      
      
    } else {leafletProxy("map")%>%
        hideGroup("myPolygons")
      callModule(selectMod, "eview", lf0)
    }
    
  })
}
shinyApp(ui = ui, server = server)