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

mapboxgl_proxy bugs #35

Closed benmbutler closed 2 months ago

benmbutler commented 2 months ago

Hi Kyle,

Loving mapgl so far and the material from your workshops! Thanks so much for all the hard work on it.

I've been playing with mapboxgl_proxy in a shiny app I'm that building, and came across a couple of (what I think are) bugs. Here's a reprex to help explain:

library(shiny)
library(mapgl)
library(sf)
library(dplyr)

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

ui <- fillPage(
  div(
    selectInput(
      "select_polygon",
      label = "Select a polygon",
      choices = unique(nc$NAME)
    ),
    style = "position: absolute; top: 10px; left: 10px; z-index: 1000;"
  ),
  tagList(
    mapboxglOutput("map", width = "100%", height = "100vh")
    )
)

server <- function(input, output, session) {

  selected_polygon_reactive <- reactiveVal(NULL)

  output$map <- renderMapboxgl({

    initial_data <- nc %>% filter(NAME == "Ashe")

    mapboxgl(
      style = mapbox_style("satellite-streets"),
      center = c(-2, 53),
      zoom = 7,
      access_token = app_config$mapbox_key
    ) |>
      add_fill_layer(
        id = "chosen_polygon",
        source = initial_data,
        fill_opacity = 1,
        fill_color = "red",
        fill_outline_color = "black",
        hover_options = list(
          fill_color = "white",
          fill_opacity = 0.5,
          fill_outline_color = "yellow"
        )
      ) |>
      add_symbol_layer(
        id = "chosen_polygon_label",
        source = initial_data,
        text_field = get_column("NAME"),
        text_size = 24
      ) |>
      fit_bounds(
        nc,
        padding = 50,
        animate = TRUE
      )

  })

  observeEvent(input$select_polygon, {

    hold <- nc %>% filter(NAME == input$select_polygon)

    selected_polygon_reactive(hold)

  }, ignoreInit = TRUE)

  observeEvent(selected_polygon_reactive(), {

    mapboxgl_proxy("map") |>
      clear_layer("chosen_polygon") |>
      clear_layer("chosen_polygon_label") |>
      add_fill_layer(
        id = "chosen_polygon",
        source = selected_polygon_reactive(),
        fill_opacity = 1,
        fill_color = "red",
        fill_outline_color = "black",
        hover_options = list(
          fill_color = "white",
          fill_opacity = 0.5,
          fill_outline_color = "yellow"
        )
      ) |>
      add_symbol_layer(
        id = "chosen_polygon_label",
        source = selected_polygon_reactive(),
        text_field = get_column("NAME"),
        text_size = 24
      ) |>
      fit_bounds(
        nc,
        padding = 50,
        animate = TRUE
      )

  })

}  

runApp(shinyApp(ui, server))

The app should behave in the following way: When changing the selectInput, the data gets filtered to the selected polygon, the map gets cleared, and the newly selected polygon along with its label gets shown.

When running this app on the latest CRAN release, it loads fine. However, when changing the selectInput, the "chosen_polygon" fill layer gets cleared and updated fine, but the "chosen_polygon_label" only gets removed and does not show up for the new polygon.

When running this app on the latest development version from github, the "chosen_polygon" and "chosen_polygon_label" both get cleared but neither the polygon nor the label show up.

I'm using an intermediary selected_polygon_reactive() in this example because in my actual app I'm retrieving data from a database in response to a selectInput, and therefore don't load all the data I need into the app on first load. Therefore I can't use the filtering functionality that might otherwise be an option.

Any help would be much appreciated! Thanks in advance

enriquevaa commented 2 months ago

I have the same bug; since I updated the developer version, every time I add polygons to the mapbox_proxy() they don´t appear anymore.

walkerke commented 2 months ago

Thanks to you both- I will fix ASAP!

walkerke commented 2 months ago

I've got this working now - there were two separate bugs:

benmbutler commented 2 months ago

Brilliant, thanks Kyle!