Using `tm_view(set_view = ...)` in `tmapProxy()` to centre view on clicked coordinates
This is probably a question more than a bug report, as I suspect I might not be combining functions in the right way.
I thought I could use tm_view(set_view = ...) in tmapProxy() to centre the map on the clicked coordinates, in the same way as I can remove a layer with tm_remove_layer() and add new layers. But that didn't work.
This is the Shiny application code, which doesn't throw warnings / errors but does not centre the map on every click:
library(shiny)
library(tmap)
library(sf)
tmap_mode("view")
# create initial point data
click_point <- st_point(c(153.004, -27.498))
click_sf <- st_sfc(click_point, crs = "WGS84")
# UI
ui <- fluidPage(
tmapOutput("map")
)
server <- function(input, output, session) {
# draw tmap
output$map <- renderTmap({
tm_shape(click_sf) +
tm_dots(zindex = 401)
})
# observe click
observeEvent(input$map_click, {
click_point <- st_point(c(input$map_click$lng, input$map_click$lat))
click_sf <- st_sfc(click_point, crs = "WGS84")
# remove and redraw layer
tmapProxy("map", session, {
tm_remove_layer(401) +
tm_shape(click_sf) +
tm_dots(zindex = 401) +
# ... and try to centre view on click coordinates
tm_view(set_view = c(input$map_click$lng,
input$map_click$lat,
5))
})
})
}
shinyApp(ui, server)
This is using the current dev source (4.0.0.9000).
Good question. Do you know if it's possible to automatically pan to clicked locations in a shiny app via proxy? @tim-salabim
No, I am really no expert on shiny and proxy objects. I would probably do this via htmlwidgets::onRender using some simple JS code. If you want, I can whip up a basic example
Thanks. For me it's a nice-to-have with currently a low priority.