crazycapivara / mapboxer

An R Interface to Mapbox GL JS
https://crazycapivara.github.io/mapboxer/
Other
55 stars 7 forks source link

render mapboxer on shiny #79

Open Jorge-hercas opened 3 years ago

Jorge-hercas commented 3 years ago

Hello everyone, i have a problem to render a filtered mapboxer on shiny. I get the error Problem withmutate()columncolor. e[34mℹe[39m color = pal(pop_est). e[31mxe[39m unused argument (pop_est) and i don't now why.

Any ideas or suggestions? Thanks for reading me The code is the following:

data("World")

ui <- fluidPage(

  selectInput("var","escoge un continente",
              choices = list("Asia" = "Asia",
                             "Africa" = "Africa",
                             "Europa" = "Europe",
                             "America" = "North America")       
  ),
  mapboxerOutput("map")

)

server <- function(input, output){

  dato <- reactive({

    World |> 
      filter(continent == input$var)

  })

  pal <- reactive({
    scales::col_quantile("Greys",  dato()$pop_est)
  })

  bbox <- reactive({
    unname(sf::st_bbox(dato() ))
  })

  output$map <- renderMapboxer({

    dato() %>%
      dplyr::mutate(color = pal(pop_est)) %>%
      as_mapbox_source() %>%
      mapboxer(bounds = bbox(), pitch = 25, zoom = 10) %>%
      add_navigation_control() %>%
      add_fill_layer(
        fill_color = c("get", "color"),
        fill_opacity = 0.5,
        fill_outline_color = "white",
        popup = "Población estimada: {{pop_est}}"
      )

  })

}

shinyApp(ui, server)
crazycapivara commented 3 years ago

Hi @Jorge-hercas If you want to update / filter an already rendered map it is recommended to use use mapboxer_proxy and update_mapboxer in conjunction with the filter parameter. To update the extend you can use fit_bounds.

Jorge-hercas commented 3 years ago

Thanks a lot for the answer I tried it but I get an error again :(

The code is the following:

data("World")
ui <- fluidPage(
  selectInput("var","escoge un continente",
              choices = list("Asia" = "Asia",
                             "Africa" = "Africa",
                             "Europa" = "Europe",
                             "America" = "North America")

  ),
  mapboxerOutput("map")

) 

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

  dato <- reactive({

    World |> 
      filter(continent == input$var)

  })

  pal <- reactive({
    scales::col_quantile("Greys",  dato()$pop_est)
  })

  bbox <- reactive({
    unname(sf::st_bbox(dato() ))
  })

  output$map <- mapboxer::renderMapboxer({
    mapboxer(bounds = bbox(), pitch = 25, zoom = 10)
  })

  observeEvent(input$var, {    
    dplyr::mutate(color = pal(pop_est ))  |> 
      as_mapbox_source(dato ) |> 
      add_navigation_control() |> 
      add_fill_layer(
        fill_color = c("get", "color"),
        fill_opacity = 0.5,
        fill_outline_color = "white",
        popup = "Población estimada: {{pop_est}}"
      )
  })    

}

shinyApp(ui, server)
crazycapivara commented 3 years ago

Please see this example