Skip to content

Edit existing feature within Shiny and save #105

@RossPitman

Description

@RossPitman

I'm trying to create a shiny app with mapedit that will allow me to load a feature (in this example it is the nc.shp from the sf package as an example), and then edit that same feature using mapedit. Currently, I cannot edit it the original feature, but I can edit new features that I create. Is mapedit capable of editing the original feature? It would seem unfortunate if it couldn't, and if so, is there any way to get around this using mapedit?

Thanks very much for the great package!

Reproducible code below as an example. Many thanks!

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

# Load the sf object
nc <- st_read(
  system.file(
    "shape/nc.shp", 
    package = "sf"
  )
)

# Project transformation
nc <- st_transform(
  nc, 
  crs = 4326
)

map <- leaflet() %>% 
  leaflet::addPolylines(
    data = nc,
    weight = 3,
    opacity = 1,
    fill = FALSE,
    color = 'black',
    fillOpacity = 1,
    smoothFactor = 0.01
  )

ui <- fluidPage(
  
  # Application title
  titlePanel("Test Shiny Leaflet Mapedit"),
  
  sidebarLayout(
    sidebarPanel(
      actionButton('save', 'Save edits')
    ),
    
    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 = FALSE, 
        delete_dsn = TRUE
      )
    }
    
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions