Skip to content

added _hover and _feature_hover as shiny inputs for maplibre - #111

Closed
kmcd39 wants to merge 1 commit into
walkerke:mainfrom
kmcd39:added-hover
Closed

added _hover and _feature_hover as shiny inputs for maplibre#111
kmcd39 wants to merge 1 commit into
walkerke:mainfrom
kmcd39:added-hover

Conversation

@kmcd39

Copy link
Copy Markdown
Contributor

Added hover and hover_feature as Shiny inputs from maplibre widgets.

Something i requested in an issue #109 .

A quick repex:

library(sf)
library(tidyverse)
library(mapgl)
#devtools::load_all()
statesf <- tigris::states(
) |>
rename_with(tolower)
statesf <- statesf |>
st_transform(4326)
#statesf |> tibble() |> select(geoid, name) |> arrange(geoid) |> print(n=60)
l48 <- statesf |>
filter(as.numeric(geoid) %in% 4:56)
mapgl_hover_UI <- function(id) {
ns <- NS(id)
tagList(
mapgl::maplibreOutput( ns("map")
,height = "500px")
)
}
mapgl_hover_Server <- function(id) {
moduleServer(
id,
function(input, output, session) {
output$map <- mapgl::renderMaplibre({
maplibre( ) |>
mapgl::fit_bounds(l48
) |>
mapgl::add_fill_layer(
id = "states"
,source = statesf
,fill_color =
mapgl::match_expr(
column = "division"
,values = sort(unique(statesf$division))
,stops = RColorBrewer::brewer.pal(10,"Set3")
)
)
})
observeEvent(input$map_feature_hover, {
req(input$map_feature_hover$layer == "states")
#browser()
cat("hovering over", input$map_feature_hover$properties$name, "!\n")
})
observeEvent(input$map_hover, {
cat("hover lng:",input$map_hover$lng)
})
}
)
}
shiny::shinyApp(mapgl_hover_UI("dev"),
server = function(input,output,session) mapgl_hover_Server("dev"))

@walkerke

Copy link
Copy Markdown
Owner

Awesome, I'll take a look!

@walkerke

Copy link
Copy Markdown
Owner

Great work! I am closing this in favor of #114 as I want these inputs to be turned off by default (there's a chance that firing so many map.queryRenderedFeatures() calls will slow down maps with lots of data). You can enable it by calling enable_shiny_hover() on your map, it works great in testing.

Sign up for freeto 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

@kmcd39@walkerke