leafpm
leafpm copied to clipboard
Editing existing shape does not pass reactives to Shiny
I found that when editing a shape that was added to the leaflet
instance (not drawn with the toolbar) can be edited, but the edited shape does not show up as a reactive value in a shiny app. Am I doing something wrong here?
Example app below:
- An existing polygon is plotted on the map, we can edit this by clicking the edit button of the
pmToolbar
, but nothing shows up ininput
(see Debug tab for all reactives). - If we draw a new polygon, and start editing it, it does show up as a bunch of reactive values (
main_map_draw_edited_features
).
I also noticed that if we do the following: leaflet() %>% addPolygons(data = existing_shape) %>% addPmToolbar()
, the shape is not editable, but if we do leaflet() %>% addPmToolbar() %>% addPolygons(data = existing_shape)
, the shape does become editable. Perhaps this is a feature, not a bug?
existing_shape <- structure(list(structure(list(list(structure(c(6.56404800296482,
6.62588800302508, 6.5413740029429, 6.53793800293931, 6.56267400296326,
6.56404800296482, 53.1583820045482, 53.1620880045473, 53.1703200045554,
53.1534410045469, 53.1423220045401, 53.1583820045482), .Dim = c(6L,
2L)))), class = c("XY", "MULTIPOLYGON", "sfg"))), n_empty = 0L, crs = structure(list(
epsg = 4326L, proj4string = "+proj=longlat +datum=WGS84 +no_defs"), class = "crs"), class = c("sfc_MULTIPOLYGON",
"sfc"), precision = 0, bbox = structure(c(xmin = 6.53793800293931,
ymin = 53.1423220045401, xmax = 6.62588800302508, ymax = 53.1703200045554
), class = "bbox"))
library(shiny)
library(leafpm)
library(shiny)
ui <- fluidPage(
tabsetPanel(
tabPanel("Map",
leafletOutput("main_map")
),
tabPanel("Debug",
verbatimTextOutput("debug_out")
)
)
)
server <- function(input, output, session) {
output$debug_out <- renderPrint(reactiveValuesToList(input))
output$main_map <- renderLeaflet({
leaflet() %>%
leafpm::addPmToolbar(
drawOptions = pmDrawOptions(snappable = FALSE),
toolbarOptions = pmToolbarOptions(
editMode = TRUE,
drawMarker = TRUE,
drawPolygon = TRUE,
drawCircle = FALSE,
drawPolyline = FALSE,
drawRectangle = FALSE,
cutPolygon = FALSE,
position = "bottomleft"
)
) %>%
addPolygons(data = existing_shape)
})
}
shinyApp(ui, server)
Looks like a related issue reported at https://github.com/r-spatial/mapedit/issues/113. leafpm
needs to better support new layers added to leaflet post-initial render.
This seems to be working now: an existing shape also shows up under the reactive input$mypolygon_edited_features
.