walkerke / mapgl

R interface to Mapbox GL JS v3 and Maplibre GL JS
https://walker-data.com/mapgl
Other
91 stars 5 forks source link

Clear_markers() doesn´t erase the markers in the shiny session #25

Closed enriquevaa closed 4 months ago

enriquevaa commented 4 months ago

I have written a simple code of an app that asks the user the lat and long of a point, then the user can plot it in the map through a button and also erase it through another button; however, the function clear_markers() doesn´t erase the marker in the proxy map. I have tried clear_layer() too but neither works.

Here is my code, cheers:

ui <- dashboardPage(skin="red",
                    dashboardHeader(title="Mapa interactivo"),
                    dashboardSidebar(
                      sidebarMenu(menuItem(
                        "Análisis", 
                        tabName = "isos", 
                        icon = icon("map-pin"),
                        textInput("latitud", "Latitud:",width = "250px"),
                        textInput("longitud", "Longitud:",width = "250px"),
                        actionButton("agregar", "Nuevo punto",width = "192px"),
                        actionButton("buffer", "Buffer (400m)",width = "192px"),
                        br(),
                        actionButton("borrar", "Borrar",style=estilo,width = "192px")))
                      ,collapsed=T),
                    dashboardBody(
                      tags$style(type = "text/css", "#mapa {height: calc(100vh - 80px) !important;}"),
                      mapboxglOutput("mapa")
                    ))

server <- function(input, output, session) {
  markers <- reactiveVal(data.frame(lat = numeric(0), lon = numeric(0),idp=numeric(0)))
  buffer_layer <- reactiveVal(NULL)

  output$mapa <- renderMapboxgl({
    mapboxgl(center = c( -99.16719645546601,19.427190799035163),zoom=10,style = mapbox_style("dark"),access_token = token) })

    observeEvent(input$agregar, {
      lat <- as.numeric(input$latitud)
      lon <- as.numeric(input$longitud)

      #current_markers <- markers()
      #new_marker <- data.frame(lat = lat, lon = lon)
      #markers(rbind(current_markers, new_marker))

      if (!is.na(lat) && !is.na(lon)) {
        current_markers <- markers()
        new_marker <- data.frame(lat = lat, lon = lon,idp=1)
        markers(rbind(current_markers, new_marker))
        #coords <- markers()
        points_sf <- st_as_sf(markers(), coords = c("lon", "lat"), crs = 4326)
        mapboxgl_proxy("mapa") %>% add_markers(points_sf,marker_id="idp")
      } else {
        showModal(modalDialog(
          title = "Error",
          "Por favor, ingresa valores válidos para latitud y longitud."
        ))
      }
    })

    observeEvent(input$buffer, {
      if (nrow(markers()) > 0) {
        coords <- markers()
        sf_use_s2(T)
        points_sf <- st_as_sf(coords, coords = c("lon", "lat"), crs = 4326)
        buffer_sf <- st_buffer(points_sf, dist = 400)
        buffer_sf$Clave_buffer<-rownames(buffer_sf)
        sf_use_s2(F)

        mapboxgl_proxy("mapa") %>%
          add_fill_layer(id="Clave_buffer",source=buffer_sf,fill_color  = "blue", fill_opacity = 0.2)
      } else {
        showModal(modalDialog(
          title = "Error",
          "Por favor, agrega al menos un marcador antes de crear el buffer."
        ))
      }
    })

    observeEvent(input$borrar, {
      mapboxgl_proxy("mapa") %>% clear_layer("Clave_buffer") %>% clear_markers()
      markers(data.frame(lat = numeric(0), lon = numeric(0)))
      buffer_layer(NULL)
    })}

shinyApp(ui,server)
walkerke commented 4 months ago

I couldn't run your app but I found the bug and fixed it - can you try re-installing and see if it works now?

enriquevaa commented 4 months ago

Perfect Kyle! Works now!!