I have a Shiny app with tmap and want the user's zoom and pan to update the data filter. For instance:
require(tmap)
require(shiny)
require(dplyr)
d = readr::read_csv('https://gist.githubusercontent.com/curran/13d30e855d48cdd6f22acdf0afe27286/raw/0635f14817ec634833bb904a47594cc2f5f9dbf8/worldcities_clean.csv') |>
group_by(lat = 3*round(lat/3), lng = 3*round(lng/3)) |>
summarise(n = length(lat), .groups = 'drop')
server = function(input, output, session) {
tmap_mode("view")
output$map = renderTmap({
# I want bbx to reflect the current leaflet bounding box
bbx = list(top = 70, right = 60, bottom = 20, left = -20)
p = d |> filter(lat > bbx$bottom, lat < bbx$top, lng > bbx$left, lng < bbx$right) |>
sf::st_as_sf(coords = c('lng', 'lat'), crs = 'WGS84')
tm_basemap("Stamen.TonerLite") +
tm_shape(p) +
tm_symbols(size="n", col = 'n', border.col = 'white', # shape=15,
palette = c('navy','red'),
legend.size.show = FALSE, scale=1, id="key", popup.vars = TRUE)
})
}
ui = mapUI = fluidPage(
tmapOutput('map', width = '95vw', height = '95vh')
)
shinyApp(ui, server, options = list(launch.browser = FALSE))
How can the bbx
object capture the current leaflet bounding box? tmap
doesn't seem to offer anything that looks like event capture and I've not managed to pin down a client-side js method for this (or hack one myself) but presumably it's possible.