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

Changing `fill-color` with `set_paint_property()` removes `fill_color` in `hover_options` #41

Closed stefanlinner closed 2 months ago

stefanlinner commented 2 months ago

Hi,

I think I'm experiencing a bug. When clicking the "Click" button, the fill color should change to interpolate from PERIMETER instead of AREA. This works as intended. However, after clicking the "Click" button, the yellow fill color of hover_options disappeared.

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

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

ui <-
  fluidPage(
    actionButton("click", "Click"),
    mapboxglOutput("map")
  )

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

  output$map <- renderMapboxgl({
    mapboxgl() %>%
      fit_bounds(
        nc,
        animate = FALSE
      ) |>
      add_fill_layer(
        "fill",
        source = nc,
        fill_color = interpolate(
          column = "AREA",
          values = c(min(nc[["AREA"]]), max(nc[["AREA"]])),
          stops = c("#f7fbff", "#08306b"),
          na_color = "lightgrey"
        ),
        hover_options = list(
          fill_color = "yellow"
        )
      )
  })

  myMapProxy <- mapboxgl_proxy("map", session)

  observeEvent(input$click, {
    myMapProxy |>
      set_paint_property(
        "fill",
        "fill-color",
        interpolate(
          column = "PERIMETER",
          values = c(min(nc[["PERIMETER"]]), max(nc[["PERIMETER"]])),
          stops = c("#f7fbff", "#08306b"),
          na_color = "lightgrey"
        )
      )
  })
}

shinyApp(
  ui = ui,
  server = server
)

Thanks a lot for your work! The package is great!

walkerke commented 2 months ago

Thanks for the note! This is a bug, I've noticed it too in one of my own projects. I'll think through the best way to handle this.

walkerke commented 2 months ago

Just fixed this for both Mapbox and MapLibre. Test it out and let me know if you see any further issues!

stefanlinner commented 2 months ago

Works fine now, thanks for the quick fix!