Skip to content

Memory Leak Issue with maplibre_proxy, clear_layer(), and clear_legend() #73

@JMTocho

Description

@JMTocho

Hi! this package it's great! thanks!

Im having a memory leak in a shinyapp when using maplibre_proxy along with the functions clear_layer() and clear_legend(). Every time the user applies different filters, RAM usage increases. Initially, this is not a problem, but as more queries are made, memory consumption grows continuously until the application crashes. Here a small example(mine works with more data):

library(shiny)
library(bslib)
library(colourpicker)
library(dplyr)
library(sf)
library(shinyWidgets)
library(mapgl)

nc <- st_read(system.file("shape/nc.shp", package = "sf"),
              quiet = TRUE)

ui <- bootstrapPage(
  pickerInput(inputId = 'var',
              label = 'Ver',
              choices = c("AREA","PERIMETER"),
              options = list(`style` = "btn-info")),
  maplibreOutput("map")
)



server <- function(input, output, session) {
  
  
  output$map <- renderMaplibre({
    
    
    maplibre() |>
      fit_bounds(nc, animate = FALSE)|>
      add_fill_layer(
        id = "polygon_layer",
        source = nc,
        fill_color = "blue",
        fill_opacity = 0.5,
        tooltip = "AREA"
      )
  })
  
  

    observe({
    df2 <- nc |> 
    dplyr::select(input$var)


    
    col_indicador <- colnames(df2)[1]
    minimo <- min(df2[[1]], na.rm = TRUE)
    maximo <- max(df2[[1]], na.rm = TRUE)
    
    valores <- seq(minimo, maximo, length.out = 5)
    cols <- c("#8B2500", "#EE4000", "#9AFF9A", "#3B8C4D","#09622A")
      

    
    maplibre_proxy("map") |>
      clear_layer("polygon_layer")|>
      clear_legend()|>
      add_fill_layer(id = "polygon_layer",
                     source = df2,
                     fill_outline_color = "black",
                     fill_color = interpolate(
                       column = col_indicador,
                       property = NULL,
                       values = valores,
                       stops = cols,
                       na_color = "black"
                     ),
                     fill_opacity = 0.7,
                     hover_options = list(
                       fill_color = "yellow",
                       fill_opacity = 1
                     ))|>
      add_legend(legend_title = col_indicador, colors = cols,
                 values = valores, position = "top-right",
                 type = "categorical")

})
}

shinyApp(ui, `server)`

Am I using maplibre_proxy, clear_layer(), and clear_legend() correctly?
How can I properly release memory to prevent crashes?
Is my logic correct to prevent RAM usage from growing indefinitely?
I am testing this with profiling—what should I look for to diagnose the issue?

Mapgl version : 0.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions