Hi,
I have the following use case: A user can change the fill_color variable by clicking a button. This already works fine using the set_paint_property() function. However, I would also like to set a new tooltip variable (as suggested by the outcommented code). It would be great if this could be done using a set_*() function that does not require redrawing the fill_layer. Currently I'm accomplishing this by first clear_layer("fill") and then again add_fill_layer(...) with the updated tooltip. However, this results in redrawing the fill_layer and with a set-update it would be much nicer and more performant.
library(mapgl)
library(sf)
library(shiny)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
ui <-
fluidPage(
actionButton("click", "Click"),
mapboxglOutput("map")
)
server <- function(input, output, session) {
output$map <- renderMapboxgl({
mapboxgl() %>%
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 = "AREA"
)
})
myMapProxy <- mapboxgl_proxy("map", session)
observeEvent(input$click, {
myMapProxy |>
set_paint_property(
"fill",
"fill-color",
interpolate(
column = "PERIMETER",
values = c(min(nc[["PERIMETER"]]), max(nc[["PERIMETER"]])),
stops = c("#f7fbff", "#08306b"),
na_color = "lightgrey"
)
) #|>
# set_*_property(
# "fill",
# "tooltip",
# "PERIMETER"
# )
})
}
shinyApp(
ui = ui,
server = server
)
Out of curiosity: Is it generally also possible to update the source dataset using a set_* function?
Thanks again for your great work, very appreaciated!
Hi,
I have the following use case: A user can change the
fill_colorvariable by clicking a button. This already works fine using theset_paint_property()function. However, I would also like to set a newtooltipvariable (as suggested by the outcommented code). It would be great if this could be done using aset_*()function that does not require redrawing thefill_layer. Currently I'm accomplishing this by firstclear_layer("fill")and then againadd_fill_layer(...)with the updated tooltip. However, this results in redrawing thefill_layerand with a set-update it would be much nicer and more performant.Out of curiosity: Is it generally also possible to update the
sourcedataset using aset_*function?Thanks again for your great work, very appreaciated!