Skip to content

set_style() reverts back to original map in shiny #39

@benmbutler

Description

@benmbutler

Hi Kyle,

I'm looking to update the basemap style using mapboxgl_proxy() and set_style() in a shiny app. The style changes fine, but the map seems to revert back to the original data that was displayed when it was first rendered.

For instance, in the reprex below, if you choose a different polygon and then update the style to "satellite-streets", the polygon and label data will disappear altogether at first, but when changing the style back to "standard" will revert back to the data that was initially provided to renderMapboxgl().

Ideally I'd like set_style() to just change the basemap without affecting any of the data that's loaded:

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)
    ),
    selectInput("style", "Choose Map Style:",
                choices = c("Standard" = "standard", 
                            "Satellite Streets" = "satellite-streets")),
    style = "position: absolute; top: 10px; left: 10px; z-index: 1000;"
  ),
  tagList(
    mapboxglOutput("map", width = "100%", height = "100vh")
  )
)

server <- function(input, output, session) {
  
  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)
    
    mapboxgl_proxy("map") |>
      clear_layer("chosen_polygon") |>
      clear_layer("chosen_polygon_label") |>
      add_fill_layer(
        id = "chosen_polygon",
        source = hold,
        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 = hold,
        text_field = get_column("NAME"),
        text_size = 24
      ) |>
      fit_bounds(
        nc,
        padding = 50,
        animate = TRUE
      )
    
  })
  
  observeEvent(input$style, {
    mapboxgl_proxy("map") %>%
      set_style(mapbox_style(input$style))
  })
  
}  

runApp(shinyApp(ui, server))

I'm using the dev version of mapgl that I downloaded yesterday. Any solution would be much appreciated! Thanks in advance!

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