Skip to content

Conversation

@kmcd39
Copy link
Contributor

@kmcd39 kmcd39 commented Jan 3, 2025

At title says, i adapted @stefanlinner's code to make set_tooltips/source work on maplibre side. (issue #42)

i'm pretty extremely new to JS but functions are tested in a minimal shiny app:

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

nc <- st_read(system.file("shape/nc.shp", package="sf")) %>%
  mutate(label = paste0(NAME, "<br>", AREA, "<br>", PERIMETER)) |>
  mutate(FIPS = as.numeric(FIPS))

maplibre.ui <-
  fluidPage(

    actionButton("clear.and.refill", "Click")
    ,checkboxInput("arbitrary.filter", "arbitrary.filter")

    ,shiny::selectizeInput("tooltip.colm", label = "tooltip column"
                           ,choices = colnames(nc)[c(1:14,16)]
                           ,selected  = "label"
    )

    ,checkboxInput("arbitrary.filter.source", "arbitrary.filter by source")

    ,maplibreOutput("map")
  )

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

  output$map <-
    renderMaplibre({

      maplibre() %>%
        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"
          )
          ,tooltip = "label"
        )
    })

  proxy <- maplibre_proxy("map", session)

  # clear and refill a bunch of times
  observeEvent(input$clear.and.refill, {

    for(i in 1:200){
      proxy |>
        clear_layer("fill") |>
        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"
          )
        )
    }

  })

  # arbitrary filter by changing source data
  observeEvent( input$arbitrary.filter.source, {

    if(input$arbitrary.filter.source) {
      x <- nc |>
        filter(FIPS >= 37100)
    } else{
      x <- nc
    }

    proxy |>
      mapgl::set_source("fill", x)
  })

  # arbitrary filter by mapgl::set_filter
  observeEvent( input$arbitrary.filter, {

    if(input$arbitrary.filter)
      proxy |>
      mapgl::set_filter(
        layer_id = "fill"
        ,filter =
          list(">=", get_column("FIPS"), 37100)
      )
    else
      proxy |>
      mapgl::set_filter(
        layer_id = "fill"
        ,filter =
          list(">=", get_column("FIPS"), 0)
      )

  })

  # do tooltip by column.
  observeEvent( input$tooltip.colm, {
    req(proxy)

    proxy |>
      mapgl::set_tooltip(
        "fill"
        ,input$tooltip.colm
      )
  })
}

shinyApp(
  ui = maplibre.ui,
  server = maplibre.server
)

@walkerke
Copy link
Owner

walkerke commented Jan 3, 2025

Thank you, I'll take a look!

@walkerke walkerke merged commit 656d94e into walkerke:main Jan 7, 2025
@walkerke
Copy link
Owner

walkerke commented Jan 7, 2025

Works great! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants