保持 Tmap 重新绘制时视口不变 (R/Shiny)

huangapple go评论69阅读模式
英文:

Make Tmap keep the same viewport when redrawing (R/Shiny)

问题

我想存储Tmap中的中心坐标和缩放级别,以便在地图重绘时使用最后已知的中心点和缩放级别。当前行为是重置为初始中心点和缩放级别,如下所示:

library(shiny)
library(tmap)

data(World)
world_vars <- setdiff(names(World), c("iso_a3", "name", "sovereignt", "geometry"))

ui <- fluidPage(
  tmapOutput("map"),
  actionButton("randVar", "Random")
)

server <- function(input, output, session) {
  map.center = c(lon=-2.460938,lat=54.444492)     # 视口的中心
  map.zoom = 4                                    # 当前缩放级别

  rv <- reactiveValues(var = 1)
  
  output$map <- renderTmap({
    tmap_mode("view")
    tm_shape(World) +
      tm_polygons(world_vars[rv$var]) + 
      tm_view(set.view = c(map.center['lon'], map.center['lat'], zoom=map.zoom))
  })
  
  ## ---------------
  observeEvent(input$map_bounds, {
    map.zoom <- input$map_zoom
    map.center['lon'] <- input$map_center[['lng']]
    map.center['lat'] <- input$map_center[['lat']] 
    print(map.center)
    print(map.zoom)
  })    
  
  ## ---------------
  observeEvent(input$randVar, {
    rv$var <- sample(1:length(world_vars), 1)
  })    
}   

shinyApp(ui, server)

缩放或平移地图,然后按“Random”按钮。

期望的行为是地图重新绘制,并且set.view使用最后的中心点和缩放级别,但它重置为初始设置?

英文:

I'd like to store the centre coordinates and zoom in Tmap so that when the map gets redrawn it uses the last known centre point and zoom level. The current behaviour is that it resets to the initial centre point and zoom level as per repro:

library(shiny)
library(tmap)

data(World)
world_vars &lt;- setdiff(names(World), c(&quot;iso_a3&quot;, &quot;name&quot;, &quot;sovereignt&quot;, &quot;geometry&quot;))


ui &lt;- fluidPage(
  tmapOutput(&quot;map&quot;),
  actionButton(&quot;randVar&quot;, &quot;Random&quot;)
)

server &lt;- function(input, output, session) {
  map.center = c(lon=-2.460938,lat=54.444492)     # The center of the viewport
  map.zoom = 4                                    # The current zoom level

  rv &lt;- reactiveValues(var = 1)
  
  output$map &lt;- renderTmap({
    tmap_mode(&quot;view&quot;)
    tm_shape(World) +
      tm_polygons(world_vars[rv$var]) + 
      tm_view(set.view = c(map.center[&#39;lon&#39;], map.center[&#39;lat&#39;], zoom=map.zoom))
  })
  
  ## ---------------
  observeEvent(input$map_bounds, {
    map.zoom &lt;- input$map_zoom
    map.center[&#39;lon&#39;] &lt;- input$map_center[[&#39;lng&#39;]]
    map.center[&#39;lat&#39;] &lt;- input$map_center[[&#39;lat&#39;]] 
    print(map.center)
    print(map.zoom)
  })    
  
  ## ---------------
  observeEvent(input$randVar, {
    rv$var &lt;- sample(1:length(world_vars), 1)
  })    
}   

shinyApp(ui, server)

Zoom or pan the map and then press 'Random'.

The expected behaviour is for the map to redraw and for set.view to use the last centre point and zoom, but it resets to its initial setting?

答案1

得分: 1

    library(shiny)
    library(tmap)
    
    data(World)
    world_vars <- setdiff(names(World), c("iso_a3", "name", "sovereignt", "geometry"))
    
    ui <- fluidPage(
      tmapOutput("map"),
      actionButton("randVar", "Random")
    )
    
    server <- function(input, output, session) {
      map.center <<- c(lon=-2.460938,lat=54.444492)     # 视口中心
      map.zoom <<- 4                                    # 当前缩放级别
      
      rv <- reactiveValues(var = 1)
      
      output$map <- renderTmap({
        tmap_mode("view")
        tm_shape(World) +
          tm_polygons(world_vars[rv$var]) + 
          tm_view(set.view = c(map.center['lon'], map.center['lat'], zoom=map.zoom))
      })
      
      ## ---------------
      observeEvent(input$map_bounds, {
        map.zoom <<- input$map_zoom
        map.center['lon'] <<- input$map_center[['lng']]
        map.center['lat'] <<- input$map_center[['lat']]
        print(map.center)
        print(map.zoom)
      })    
      
      ## ---------------
      observeEvent(input$randVar, {
        rv$var <- sample(1:length(world_vars), 1)
      })    
    }   
    
    shinyApp(ui, server)
英文:
library(shiny)
library(tmap)

data(World)
world_vars &lt;- setdiff(names(World), c(&quot;iso_a3&quot;, &quot;name&quot;, &quot;sovereignt&quot;, &quot;geometry&quot;))


ui &lt;- fluidPage(
  tmapOutput(&quot;map&quot;),
  actionButton(&quot;randVar&quot;, &quot;Random&quot;)
)

server &lt;- function(input, output, session) {
  map.center &lt;&lt;- c(lon=-2.460938,lat=54.444492)     # The center of the viewport
  map.zoom &lt;&lt;- 4                                    # The current zoom level
  
  rv &lt;- reactiveValues(var = 1)
  
  output$map &lt;- renderTmap({
    tmap_mode(&quot;view&quot;)
    tm_shape(World) +
      tm_polygons(world_vars[rv$var]) + 
      tm_view(set.view = c(map.center[&#39;lon&#39;], map.center[&#39;lat&#39;], zoom=map.zoom))
  })
  
  ## ---------------
  observeEvent(input$map_bounds, {
    map.zoom &lt;&lt;- input$map_zoom
    map.center[&#39;lon&#39;] &lt;&lt;- input$map_center[[&#39;lng&#39;]]
    map.center[&#39;lat&#39;] &lt;&lt;- input$map_center[[&#39;lat&#39;]] 
    print(map.center)
    print(map.zoom)
  })    
  
  ## ---------------
  observeEvent(input$randVar, {
    rv$var &lt;- sample(1:length(world_vars), 1)
  })    
}   

shinyApp(ui, server)

huangapple
  • 本文由 发表于 2023年2月8日 18:59:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定