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

Popup legends in shiny #6

Closed enriquevaa closed 4 months ago

enriquevaa commented 4 months ago

The function hover or tooltip works fine with mapboxgl in my console, but when I render it on a shiny app it doesn´t show any legends. Cheers for all your work Kyle.

walkerke commented 4 months ago

Thank you! Can you post the exact code that isn't working along with your sessionInfo()?

enriquevaa commented 4 months ago
#Reading layers
t3b_cdmx<-st_read("t3b_cdmx.shp") %>% st_transform('+proj=longlat +datum=WGS84')
manzanas<-st_read("Hogares_AGEB_CDMX.shp")%>% st_transform('+proj=longlat +datum=WGS84')%>% st_transform(crs = 4326)

#Adding Popup
manzanas$popup <- glue::glue(
  "<strong>CVEGEO: </strong>{manzanas$CVEGEO}<br><strong>Poblacion: </strong>{manzanas$HOGAR_AGEB}"
)

#Creating ui
vars5<-c("Ninguno","Poblacion","Heatmap")

ui <- dashboardPage(skin="red",
                    dashboardHeader(title="Mapa interactivo T3B"),
                    dashboardSidebar(

                      sidebarMenu(menuItem(
                        "Capas", 
                        tabName = "lays", 
                        icon = icon("layer-group"),selectInput("layers", "Agregar capa", vars5)))),
                    dashboardBody(
                      tags$style(type = "text/css", "#mapa {height: calc(100vh - 80px) !important;}"),
                      mapboxglOutput("mapa")
                    ))

#Server

server <- function(input, output, session) {
  bins_1<- c(0,200,400,  700, 1000 ,1500 ,2000,4000,11000)
  output$mapa <- renderMapboxgl({
    mapboxgl(center = c( -99.16719645546601,19.427190799035163),zoom=10,style = mapbox_style("dark"),access_token = token)})

  observe({
    comp<- input$layers

    if (comp== "Ninguno") {
      mapboxgl_proxy("mapa") %>%  clear_layer("X.")%>%  clear_layer("CVEGEO")
    } 
    if (comp== "Heatmap") {
      mapboxgl_proxy("mapa") %>%
        add_heatmap_layer(
          id = "X.",
          source = t3b_cdmx,
          heatmap_radius=15,
          heatmap_opacity = 0.7,
          heatmap_color = interpolate(
            property = "heatmap-density",
            values = seq(0, 1, 0.2),
            stops = c('rgba(33,102,172,0)', 'rgb(103,169,207)',
                      'rgb(209,229,240)', 'rgb(253,219,199)',
                      'rgb(239,138,98)', 'rgb(227,27,35)')))
    } 
    if (comp== "Poblacion") {
      mapboxgl_proxy("mapa") %>%
        add_fill_layer(id = "CVEGEO",
                       source = manzanas,
                       fill_color =interpolate(
                         column = "HOGAR_AGEB",
                         values = bins_1,
                         stops =c('rgb(255,255,217)', 'rgb(237,248,177)',
                                  'rgb(199,233,180)', 'rgb(127,205,187)',
                                  'rgb(65,182,196)', 'rgb(29,145,192)','rgb(34,94,168)',
                                  'rgb(12,44,132)','rgb(12,44,132)'),
                         na_color = "lightgrey"
                       ),
                       popup="popup",
                       tooltip = "HOGAR_AGEB",
                       hover_options = list(
                         fill_color = "yellow",
                         fill_opacity = 1
                       ),
                       fill_opacity = 0.5) %>% 
        add_legend(
          "Hogares en AGEB",
          values = c(20, 80),
          colors = c('rgb(255,255,217)', 'rgb(12,44,132)')
        )
    }
  })}

shinyApp(ui, server)

Session info:

image

walkerke commented 4 months ago

Would you mind sharing your data? I tried an analogous example with some different data and the legend appeared for me. However, I'll also test on Windows and with your data.

enriquevaa commented 4 months ago

Yeah sure, I mean with the legend I refer to show the info as popup from the polygons added. Appreciate

Hogares_AGEB_CDMX.zip

enriquevaa commented 4 months ago

I have checked and it works if a render again de mapbox "mapa" output, but no if a use mapboxgl_proxy()

walkerke commented 4 months ago

This should now work. Can you re-install and try it out?

enriquevaa commented 4 months ago

Perfect. It works now!