只有在Shiny中缩放超过阈值时才渲染leaflet地图。

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

Only render leaflet map when zoom passes a threshold in Shiny

问题

在Shiny应用程序中,我已经构建了一个Leaflet地图,根据缩放级别的不同显示不同的图层。

我使用了leafletProxyobserveEvent以及Leaflet的input$MAPID_zoom来构建这个地图。

  1. server <- shinyServer(function(input, output, session) {
  2. output$map <- renderLeaflet({
  3. leaflet() %>%
  4. addTiles() %>%
  5. mapOptions(zoomToLimits="first") %>%
  6. setView(lat = 47.218637, lng = -1.554136, zoom = 7)
  7. })
  8. observeEvent(
  9. eventExpr = input$map_zoom, {
  10. map_proxy <- leafletProxy(mapId = "map", session = session)
  11. if(input$map_zoom < 8){
  12. map_proxy %>%
  13. clearMarkers() %>%
  14. addMarkers(
  15. data = df, lng = ~lng, lat = ~lat
  16. )
  17. } else {
  18. map_proxy %>%
  19. clearMarkers() %>%
  20. addMarkers(
  21. data = df_2, lng = ~lng, lat = ~lat
  22. )
  23. }
  24. }
  25. )
  26. })

然而,这会在缩放级别变化时重新渲染Leaflet地图。我希望地图只在if语句中的阈值被超过时才重新渲染。有什么方法可以解决这个问题?

UI和数据的代码如下:

  1. library(shiny)
  2. library(leaflet)
  3. df <- data.frame(
  4. location_name = c('S1', 'S2'),
  5. lng = c(-1.554136, -2.10401),
  6. lat = c(47.218637, 47.218637),
  7. stringsAsFactors = FALSE
  8. )
  9. df_2 <- data.frame(
  10. location_name = c('S3', 'S4'),
  11. lng = c(-1.654136, -2.2401),
  12. lat = c(47.218637, 47.218637),
  13. stringsAsFactors = FALSE
  14. )
  15. ui <- shinyUI(
  16. fluidPage(
  17. leafletOutput(outputId = 'map')
  18. )
  19. )
英文:

I have built a leaflet map in a Shiny application that shows different layers depending on what zoom you have.

I have built this combining leafletProxy with observeEvent and leaflet's input$MAPID_zoom.

  1. server &lt;- shinyServer(function(input, output, session) {
  2. output$map &lt;- renderLeaflet({
  3. leaflet() |&gt;
  4. addTiles() |&gt;
  5. mapOptions(zoomToLimits=&quot;first&quot;) |&gt;
  6. setView(lat = 47.218637, lng = -1.554136, zoom = 7)
  7. })
  8. observeEvent(
  9. eventExpr = input$map_zoom, {
  10. map_proxy &lt;- leafletProxy(mapId = &quot;map&quot;, session = session)
  11. if(input$map_zoom &lt; 8){
  12. map_proxy |&gt;
  13. clearMarkers() |&gt;
  14. addMarkers(
  15. data = df, lng = ~lng, lat = ~lat
  16. )
  17. } else {
  18. map_proxy |&gt;
  19. clearMarkers() |&gt;
  20. addMarkers(
  21. data = df_2, lng = ~lng, lat = ~lat
  22. )
  23. }
  24. }
  25. )
  26. })

However, this rerenders the leaflet map every time the zoom changes. I would like the map to only render when the threshold in the if-statement is surpassed. What would be a way to approach this?

Code for UI and data:

  1. library(shiny)
  2. library(leaflet)
  3. df &lt;- data.frame(
  4. location_name = c(&#39;S1&#39;, &#39;S2&#39;),
  5. lng = c(-1.554136, -2.10401),
  6. lat = c(47.218637, 47.218637),
  7. stringsAsFactors = FALSE
  8. )
  9. df_2 &lt;- data.frame(
  10. location_name = c(&#39;S3&#39;, &#39;S4&#39;),
  11. lng = c(-1.654136, -2.2401),
  12. lat = c(47.218637, 47.218637),
  13. stringsAsFactors = FALSE
  14. )
  15. ui &lt;- shinyUI(
  16. fluidPage(
  17. leafletOutput(outputId = &#39;map&#39;)
  18. )
  19. )

答案1

得分: 1

我无法重现你的问题,但这里有一个解决方案。
你需要创建两个图层并调整可见性。

  1. server <- shinyServer(function(input, output, session) {
  2. output$map <- renderLeaflet({
  3. leaflet() %>%
  4. addTiles() %>%
  5. mapOptions(zoomToLimits="first") %>%
  6. setView(lat = 47.218637, lng = -1.554136, zoom = 7) %>%
  7. addMarkers(
  8. data = df, lng = ~lng, lat = ~lat, group = "S1S2"
  9. ) %>%
  10. addMarkers(
  11. data = df_2, lng = ~lng, lat = ~lat, group="S3S4")
  12. })
  13. observeEvent(
  14. eventExpr = input$map_zoom, {
  15. map_proxy <- leafletProxy(mapId = "map", session = session)
  16. if(input$map_zoom < 8){
  17. map_proxy %>%
  18. showGroup("S1S2") %>%
  19. hideGroup("S3S4")
  20. } else {
  21. map_proxy %>%
  22. showGroup("S3S4") %>%
  23. hideGroup("S1S2")
  24. }
  25. }
  26. )
  27. })

在这里,每个图层都有不同的组ID,根据缩放级别,我们隐藏或显示相应的组。

英文:

I can't reproduce your problem but here is a solution.
You have to create both layers and play with visibility.

  1. server &lt;- shinyServer(function(input, output, session) {
  2. output$map &lt;- renderLeaflet({
  3. leaflet() |&gt;
  4. addTiles() |&gt;
  5. mapOptions(zoomToLimits=&quot;first&quot;) |&gt;
  6. setView(lat = 47.218637, lng = -1.554136, zoom = 7) |&gt;
  7. addMarkers(
  8. data = df, lng = ~lng, lat = ~lat, group = &quot;S1S2&quot;
  9. ) |&gt;
  10. addMarkers(
  11. data = df_2, lng = ~lng, lat = ~lat, group=&quot;S3S4&quot;)
  12. })
  13. observeEvent(
  14. eventExpr = input$map_zoom, {
  15. map_proxy &lt;- leafletProxy(mapId = &quot;map&quot;, session = session)
  16. if(input$map_zoom &lt; 8){
  17. map_proxy |&gt;
  18. showGroup(&quot;S1S2&quot;) |&gt;
  19. hideGroup(&quot;S3S4&quot;)
  20. } else {
  21. map_proxy |&gt;
  22. showGroup(&quot;S3S4&quot;) |&gt;
  23. hideGroup(&quot;S1S2&quot;)
  24. }
  25. }
  26. )
  27. })

Here each layer gets a different group id and depending of the zoom level we hide or show corresponding group.

huangapple
  • 本文由 发表于 2023年6月26日 17:44:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555480.html
匿名

发表评论

匿名网友

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

确定