Closed GreyMerchant closed 5 years ago
@GreyMerchant thanks so much for the issue. There are different opinions on whether a question like this belongs in Github issues, but for mapedit
Github issues is the encouraged forum. I'm travelling currently, but I'll try to post an example once I get back fully online. Glad you are using mapedit
!
Hi @GreyMerchant I hope this is what you are talking about. Below is my minimum example of using mapedit and leaflet with a button to save edits. In this case the button will also save the created spatial dataframe to a geojson file.
library(shiny)
library(leaflet)
library(mapedit)
library(sf)
map <- leaflet() %>%
addTiles()
ui <- fluidPage(
# Application title
titlePanel("Minimal Shiny Leaflet Mapedit"),
# Sidebar with a ui for grabbing mapedit data
sidebarLayout(
sidebarPanel(
actionButton('save', 'Save from Map')
),
# add map
mainPanel(
editModUI("map")
)
)
)
server <- function(input, output) {
edits <- callModule(
editMod,
leafmap = map,
id = "map"
)
observeEvent(input$save, {
geom <- edits()$finished
if (!is.null(geom)) {
assign('new_geom', geom, envir = .GlobalEnv)
sf::write_sf(geom, 'new_geom.geojson', delete_layer = TRUE, delete_dsn = TRUE)
}
})
}
# Run the application
shinyApp(ui = ui, server = server)
if you also wanted to manipulate the leaflet map you can use leafletProxy
in the normal way. For example:
leafletProxy("map-map") %>%
addPolygons(data = geom, color = 'red')
notice the map-map
reference? this is due the the module name spacing, so you could use ns()
to make this more dynamic.
Thank you for the responses :) This reads far easier than the code I was looking at. Thank you so much!
going to close... please reopen if we missed anything. Thanks for using mapedit
.
Hi,
Thanks for this package. The project looks amazing and will be a phenomenal help going forward. I am struggling a bit to get everything to work and I hope I may ask for help here.
So I want to get this functionality directly in Shiny:
what_we_created <- mapview() %>% editMap()
I essentially just want to be able to call up a leaflet map and allow a person to draw a spatial polygon and get the shiny application to save that information in the background.
I am able to get the package to work with this code on my server bit of my R code:
shinyServer(function(input, output, session) {
#callModule creates a small module
CM_edit <- callModule(editMod, "test-edit", m, record = TRUE)
output$edited <- renderLeaflet({
req(CM_edit()$finished)
mapview(CM_edit()$finished)@map
})
})
However it is not the same as editMap() where you can hit "done" and get it to write to a df (I am also not sure if I am doing it entirely correct above). Is there some way to have this exact functionality in shiny?
Any other help and tips would be appreciated! I am finding the syntax a bit strange still.