Dear all,
I'm trying to develop a shiny app that allows users to create polygons above buildings or fields of interest, and after doing so I would like to allow users to select one of the previously created polygons to perform an analysis.
I have problems with the selection part and I don't understand what's wrong with the selectMod module for the mapview/mapedit package.
I will insert the code
Thanks in advance to the people who will help me.
library(shiny)
library(shinydashboard)
library(sf)
library(sp)
library(leaflet)
library(leaflet.extras)
library(leafem)
library(mapview)
library(mapedit)
ui <- dashboardPage(dashboardHeader(title = "Map edit shiny app",
titleWidth = 350),
dashboardSidebar(width = 350,
sidebarMenu(
menuItem("Map", tabName = "map", icon = icon("map")))),
dashboardBody(tabItems(tabItem(tabName = "map",
fluidRow(editModUI(id="map_a"),
actionButton(inputId = "write_polygon",
label = "Save the polygon"),
selectModUI(id="select_polygon"),
uiOutput(outputId = "map_b"))))))
server <- function(input, output, session) {
map <- leaflet() %>%
addProviderTiles(providers$OpenStreetMap,
options = tileOptions(minZoom = 2, maxZoom = 15)) %>%
addProviderTiles(providers$Esri.WorldImagery,
options = tileOptions(minZoom = 15, maxZoom = 20),
group = "Esri.WorldImagery") %>%
addControlGPS(options = gpsOptions(position = "topleft",
activate = TRUE,
autoCenter = TRUE,
maxZoom = 5,
setView = TRUE)) %>%
leaflet.extras::addSearchOSM(options = searchOptions(collapsed = TRUE, autoCollapse = TRUE)) %>%
addDrawToolbar(targetGroup='drawPoly',
polylineOptions = F,
circleOptions = F,
markerOptions = F,
circleMarkerOptions = F,
rectangleOptions = F,
singleFeature = FALSE,
editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions())) %>%
addMeasure(primaryLengthUnit = "meters",
primaryAreaUnit = "hectares",
position = "topleft",
activeColor = "red",
completedColor = "red",
localization = "en")
edits <- callModule(editMod,
leafmap = map,
id = "map_a")
observeEvent(input$write_polygon, {
polygon<-edits()$finished
output$map_b<-renderUI({
mapviewOutput(outputId = "map_b_1")
actionButton(inputId = "save_selected_polygon",
label = "Save the selected polygon")
})
output$map_b_1<-renderMapview({
mapview(polygon)
})
map_to_select <- reactive({
leaflet(polygon) %>%
addPolygons() %>%
addProviderTiles(providers$OpenStreetMap)
})
selections <- callModule(selectMod,
leafmap = map_to_select(),
id = "select_polygon")
observeEvent(input$save_selected_polygon, {
final_polygon<-selections()$finished
plot(final_polygon)
})
})
}
shinyApp(ui=ui, server = server)