Comments (8)
Thanks for filing issue, and I want to make sure that I fully understand your question.
Are you asking if leafletProxy
can be used with the edit
module? If so, here is an example that might help. leafletProxy
with modules is slightly more complicated. We will need to use shiny::NS()
with the id provided in editModUI
("editme"
in example below).
library(shiny)
library(leaflet)
library(leaflet.extras)
library(mapedit)
library(mapview)
ui <- tagList(
editModUI("editme"),
actionButton("clear", "clear points"),
actionButton("remove", "remove draw")
)
server <- function(input, output, session) {
# to use the namespace that we gave
ns <- NS("editme")
callModule(
editMod,
"editme",
leaflet() %>% addFeatures(breweries)
)
observeEvent(
input$clear,
{
print(ns("map"))
# editMod uses "map" as the id for the map
leafletProxy(ns("map")) %>%
clearMarkers()
}
)
observeEvent(
input$remove,
{
leafletProxy(ns("map")) %>%
removeDrawToolbar()
}
)
}
shinyApp(ui,server)
If your question is more in regards to leafletProxy
+ Leaflet.draw
, then this very recent issue bhaskarvk/leaflet.extras#96 might be helpful.
from mapedit.
Thanks, @timelyportfolio! In hindsight my question was a bit vague, and I've edited the title to better reflect what I was actually asking. Despite the vagueness, the example you gave mostly answers what I'm looking to do. In case anyone else stumbles across this, the following slight modification allows adding and removing the editing toolbar:
library(shiny)
library(leaflet)
library(leaflet.extras)
library(mapedit)
library(mapview)
ui <- tagList(
editModUI("editor"),
actionButton("clear", "Clear Points"),
actionButton("start", "Start Drawing"),
actionButton("end", "Finish Drawing")
)
server <- function(input, output, session) {
# to use the namespace that we gave
ns <- NS("editor")
base_map <- leaflet() %>%
addTiles() %>%
addFeatures(breweries)
drawn <- callModule(editMod, "editor", base_map)
# start with drawing off
leafletProxy(ns("map")) %>%
removeDrawToolbar()
observe({print(drawn())})
observeEvent(input$clear, {
leafletProxy(ns("map")) %>%
clearMarkers()
})
observeEvent(input$start, {
leafletProxy(ns("map")) %>%
addDrawToolbar(editOptions = editToolbarOptions())
})
observeEvent(input$end, {
leafletProxy(ns("map")) %>%
removeDrawToolbar(clearFeatures = TRUE)
})
}
shinyApp(ui, server)
from mapedit.
Great and glad you asked since I think leafletProxy in general with Shiny modules does not have enough examples. Please let us know all the great things you create and please keep asking.
from mapedit.
Dear Matt and Kent,
Sorry to post my question here as the issue is now closed and my question is not (completely) related to the original question. (Let me know if I should move this post somewhere more appropriate).
I want to interactively modify a shapefile on Shiny (add and remove parts) and then save it.
I've been struggling for weeks to do it but your post gave me awesome clues. Now I can do half of what I want, I can add new features on the shapefile and save it. However, I'm still struggling with removing features of the original shapefile I loaded.
I would like to be able to modify my loaded shapefile by clicking on the bin icon and then on road segments to remove them and then save the new version of the road network (with the new parts added and without the ones removed).
Do you know if it is possible and do you have any hint on how to do this?
Here is my code.
Thanks in advance!
ui <- fluidPage(
editModUI("editor"),
downloadButton("saveChanges", "Save map modifications")
)
server <- function(input, output){
roadShp <- shapefile("data/roads.shp")
roadShpCRS <- spTransform(roadShp, CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
roadConvert <- st_as_sfc(roadShpCRS)
ns <- NS("editor")
base_map <- leaflet() %>%
addTiles() %>%
addPolylines(data = roadConvert)
drawn <- callModule(editMod, "editor", base_map)
leafletProxy(ns("map")) %>%
addDrawToolbar(editOptions = editToolbarOptions())
newRoads <- reactiveValues()
observe({
if(!is.null(drawn()))
isolate(
newRoads <<- drawn()
)
})
output$saveChanges <- downloadHandler(
filename = "newRoads.RData",
content = function(file) {
save(newRoads, file = file)
}
)
}
from mapedit.
@SarahBauduin, no need to apologize and happy you are trying mapedit
. Let's see if I might be able to help.
Do you need to specifically add the drawing toolbar? The previous example only did this since Matt wanted to be able to add and remove the toolbar. If you want it always there, then the leafletProxy
part should not be necessary.
Also, leaflet
and leaflet.extras
just got massive upgrades, so I would recommend updating to their newest versions on CRAN.
Does editFeatures()
accomplish the objective, or are you trying to integrate into a larger Shiny app?
from mapedit.
Thanks @timelyportfolio for your time!
I used the leafletProxy
part because I later added some options to only keep the line drawing tool:
leafletProxy(ns("map")) %>%
addDrawToolbar(editOptions = editToolbarOptions(),
polygonOptions = FALSE,
circleOptions = FALSE,
rectangleOptions = FALSE,
markerOptions = FALSE)
Can I add these options somewhere else without using leafletProxy
?
I installed the latest versions of leaflet
and leaflet.extras
! Thanks for the info!
The code I shared with you will be integrated into a larger Shiny app where I will, among other things, add a selectInput
to select a shapefile among severals (e.g., highways, roads, rails). Then I want the user to be able to modify the selected layer by adding or removing linear segments and then save it to use the modified layer in a model later.
I tried to reproduce this example but I couldn't figure out how to use editMap()
inside a Shiny app and then save the result in an R object.
from mapedit.
Found my solution!
I used
output$mapRoadOrigin <- renderLeaflet({
leaflet() %>%
addProviderTiles("OpenMapSurfer.Roads") %>%
addPolylines(data = roadNetwork, layerId = ~ID, color = "black", opacity = 1) %>%
addDrawToolbar(
polygonOptions = FALSE,
circleOptions = FALSE,
rectangleOptions = FALSE,
markerOptions = FALSE,
circleMarkerOptions = FALSE
)
})
# Segment selection by click
observeEvent(input$mapRoadOrigin_shape_click, {
click <- input$mapRoadOrigin_shape_click
newRoad$segmToModif <- click$id # selected segment
})
Then, I used newRoad$segmToModif
with another map in another tab to modify my road network!
from mapedit.
Hi,
The examples given above by @timelyportfolio and @mstrimas doesn't seem to work anymore. Adding and removing markers with leafletproxy still works, but removeDrawToolbar() doesn't seem to do anything.
Also, library(leafem) is required for addFeatures() but isn't listed in the example code.
from mapedit.
Related Issues (20)
- Exporting selected map data in Shiny HOT 3
- Edit existing feature within Shiny and save HOT 7
- Is there a way of flushing the old data from $finished when using singleFeature=TRUE mapedit in a Shiny app? HOT 9
- Load back previously drawn polygons to mapedit map
- editor Module in demo example fails in mapedit 0.6.0 but works in 0.5.0 HOT 2
- selectFeatures draws on world map even if crs = NA (for mode = "click" only) HOT 7
- New feature to download edited data HOT 2
- Click events for leafpm editToolbar buttons HOT 5
- leafpm removes markers added using leafletProxy HOT 2
- Use raster in base map with editMap HOT 2
- Issues installing r-spatial packages HOT 1
- is there a way to display only some of the tools HOT 2
- Is this package still actively developed? HOT 1
- Strange case of difficult viewing HOT 7
- Integration leafmap new feature HOT 17
- 'initBaseMaps' and 'mode = "click"' for selectFeatures
- Warning: `select_()` was deprecated in dplyr 0.7.0. HOT 5
- selectFeatures error possibly related to "draw" mode HOT 3
- mapedit installation on Google colab
- Error triggered by "Clear All" when there are three or more drawn objects.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mapedit.