R Shiny/R Leaflet/ R : 使用R Shiny中的sliderInput动态呈现choropleth地图 2

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

R Shiny/R Leaflet/ R : Dynamically render choropleth map with sliderInput in R shiny 2

问题

我正在尝试使用滑块功能在Leaflet地图上使用Shiny来选择并仅显示选定的多边形。以下是您代码的翻译部分:

  1. 我正在尝试使用滑块功能在Leaflet地图上使用Shiny来选择并仅显示选定的多边形。
  2. 以下是我的代码:
  3. library(shiny)
  4. library(leaflet)
  5. library(rgdal)
  6. library(sf)
  7. library(dplyr)
  8. library(RColorBrewer)
  9. # 读取波浪数据
  10. wave_data <- read_sf("~/path/Wave.shp")
  11. # 转换坐标系
  12. wave_data <- st_transform(wave_data, crs = '+proj=longlat +datum=WGS84')
  13. # 处理数据
  14. wave_data2 <- wave_data %>%
  15. mutate_at(vars(An_mn_P_OD), ~ as.integer(round(.x)))
  16. # 创建Leaflet地图
  17. wave_data_map <- leaflet() %>%
  18. addProviderTiles(providers$Esri.WorldTopoMap) %>%
  19. setView(lng = -4.2026458, lat = 56.4906712, zoom = 5)
  20. # 设置深度分级
  21. bins <- c(0, 25, 50, 100, 150, 200, 300, 400, 500, 1000, 3100)
  22. pal <- colorBin("RdYlBu", domain = wave_data2$Ave_Depth, bins = bins)
  23. # 添加多边形
  24. wave_data_map <- wave_data_map %>%
  25. addPolygons(data = wave_data2,
  26. weight = 1,
  27. smoothFactor = 0.5,
  28. color = "white",
  29. fillOpacity = 0.5,
  30. fillColor = pal(wave_data2$Ave_Depth))
  31. # 添加图例
  32. wave_data_map <- wave_data_map %>%
  33. addLegend(pal = pal,
  34. values = wave_data2$Ave_Depth,
  35. title = "Average depth",
  36. labFormat = labelFormat(suffix = "m"),
  37. opacity = 0.7,
  38. position = "bottomright")
  39. # 定义UI
  40. ui <- bootstrapPage(
  41. tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  42. leafletOutput("wave_data_map", width = "100%", height = "100%"),
  43. sliderInput("wave_data_slider", "Wave energy",
  44. min = 0, max = 75,
  45. value = c(min(wave_data2$An_mn_P_OD), max(wave_data2$An_mn_P_OD)),
  46. step = 5,
  47. round = 0.5,
  48. dragRange = TRUE)
  49. )
  50. # 定义服务器逻辑
  51. server <- function(input, output, session) {
  52. # 创建反应式对象
  53. wave_energy_output <- reactive({
  54. wave_data2 %>%
  55. filter(An_mn_P_OD >= input$wave_data_slider[1]) %>%
  56. filter(An_mn_P_OD <= input$wave_data_slider[2]) %>%
  57. group_by(Lat)
  58. })
  59. # 渲染Leaflet地图
  60. output$wave_data_map <- renderLeaflet({
  61. leaflet() %>%
  62. addProviderTiles(providers$Esri.WorldTopoMap) %>%
  63. setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %>%
  64. addPolygons(data = wave_data2,
  65. weight = 1,
  66. smoothFactor = 0.5,
  67. color = "white",
  68. fillOpacity = 0.3,
  69. fillColor = pal(wave_data2$Ave_Depth)) %>%
  70. addLegend(pal = pal,
  71. values = wave_data2$Ave_Depth,
  72. title = "Average depth",
  73. labFormat = labelFormat(suffix = "m"),
  74. opacity = 0.7,
  75. position = "bottomright")
  76. })
  77. observeEvent({input$wave_data_slider}, {
  78. data <- wave_energy_output()[wave_data2$An_mn_P_OD]
  79. leafletProxy("wave_data_map", data = wave_data2) %>%
  80. clearShapes() %>%
  81. addPolygons(data = data,
  82. fillColor = pal,
  83. weight = 0.0,
  84. opacity = 1,
  85. color = "white",
  86. dashArray = 3,
  87. fillOpacity = 0.7,
  88. ) %>%
  89. clearControls() %>%
  90. addLegend(pal = pal,
  91. values = wave_data2$Ave_Depth,
  92. title = "Average depth",
  93. labFormat = labelFormat(suffix = "m"),
  94. opacity = 0.7,
  95. position = "bottomright")
  96. })
  97. }
  98. # 运行应用
  99. shinyApp(ui = ui, server = server)

新错误提示:

警告:错误在[:]中:无法在结束之后对列进行子集选择。
ℹ 位置 43、57、57、…、48 和 46 不存在。
ℹ 只有 42 列。

希望这有所帮助。如果您需要进一步的帮助,请随时告诉我。

英文:

I am trying to use the slider function to select and only display selected polygons using Shiny on a Leaflet map.

The code below gets the slider to react, but I want the colour scale to represent the depth, and the slider to select and display the energy polygons (removing non-relevant polygons) but with the depth colour scale. The polygon files contain both the depth and energy data.

The following question points me in the right direction. But I cannot get this to work with my data.

https://stackoverflow.com/questions/53480924/dynamically-render-choropleth-map-with-sliderinput-in-r-shiny/53482005#53482005

Shapefile wave-shp (wave.shp) on the following page: https://www.renewables-atlas.info/downloads/.

Here is my code:

  1. library(shiny)
  2. library(leaflet)
  3. library(rgdal)
  4. library(sf)
  5. library(dplyr)
  6. library(RColorBrewer)
  7. wave_data &lt;- read_sf(&quot;~/path/Wave.shp&quot;)
  8. wave_data &lt;- st_transform(wave_data, crs = &#39;+proj=longlat
  9. +datum=WGS84&#39;)
  10. wave_data2 &lt;- wave_data %&gt;%
  11. mutate_at(vars(An_mn_P_OD), ~ as.integer(round(.x)))
  12. ## Load map
  13. wave_data_map &lt;- leaflet() %&gt;%
  14. addProviderTiles(providers$Esri.WorldTopoMap) %&gt;%
  15. setView(lng = -4.2026458, lat = 56.4906712, zoom = 5)
  16. wave_data_map
  17. bins &lt;- c(0, 25, 50, 100, 150, 200, 300, 400, 500, 1000, 3100)
  18. pal &lt;- colorBin(&quot;RdYlBu&quot;, domain = wave_data2$Ave_Depth, bins =
  19. bins)
  20. ## Add polygons
  21. wave_data_map &lt;- leaflet() %&gt;%
  22. addProviderTiles(providers$Esri.WorldTopoMap) %&gt;%
  23. setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %&gt;%
  24. addPolygons(data = wave_data2,
  25. weight = 1,
  26. smoothFactor = 0.5,
  27. color = &quot;white&quot;,
  28. fillOpacity = 0.5,
  29. fillColor = pal(wave_data2$Ave_Depth),
  30. )
  31. wave_data_map
  32. ## Add legend
  33. wave_data_map &lt;- leaflet() %&gt;%
  34. addProviderTiles(providers$Esri.WorldTopoMap) %&gt;%
  35. setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %&gt;%
  36. addPolygons(data = wave_data2,
  37. weight = 1,
  38. smoothFactor = 0.5,
  39. color = &quot;white&quot;,
  40. fillOpacity = 0.3,
  41. fillColor = pal(wave_data2$Ave_Depth)) %&gt;%
  42. addLegend(pal = pal,
  43. values = wave_data2$Ave_Depth,
  44. title = &quot;Average depth&quot;,
  45. labFormat = labelFormat (suffix = &quot;m&quot;),
  46. opacity = 0.7,
  47. position = &quot;bottomright&quot;)
  48. wave_data_map
  49. # Define UI for application
  50. ui &lt;- bootstrapPage(
  51. tags$style(type = &quot;text/css&quot;, &quot;html, body
  52. {width:100%;height:100%}&quot;),
  53. leafletOutput(&quot;wave_data_map&quot;, width = &quot;100%&quot;, height =
  54. &quot;100%&quot;),
  55. sliderInput(&quot;wave_data_slider&quot;, &quot;Wave energy&quot;,
  56. min = 0, max = 75,
  57. value = c(min(wave_data2$An_mn_P_OD),
  58. max(wave_data2$An_mn_P_OD)),
  59. step = 5,
  60. round = 0.5,
  61. dragRange = TRUE)
  62. ))
  63. # Define server logic
  64. server &lt;- function(input, output, session) {
  65. wave_energy_output &lt;- reactive ({
  66. wave_data2 %&gt;%
  67. filter(An_mn_P_OD &gt;= input$wave_data_range[1]) %&gt;%
  68. filter(An_mn_P_OD &lt;= input$wave_data_range[2]) %&gt;%
  69. group_by(Lat)
  70. })
  71. output$wave_data_map &lt;- renderLeaflet(
  72. leaflet() %&gt;%
  73. addProviderTiles(providers$Esri.WorldTopoMap) %&gt;%
  74. setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %&gt;%
  75. addPolygons(data = wave_data2,
  76. weight = 1,
  77. smoothFactor = 0.5,
  78. color = &quot;white&quot;,
  79. fillOpacity = 0.3,
  80. fillColor = pal(wave_data2$Ave_Depth)) %&gt;%
  81. addLegend(pal = pal,
  82. values = wave_data2$Ave_Depth,
  83. title = &quot;Average depth&quot;,
  84. labFormat = labelFormat (suffix = &quot;m&quot;),
  85. opacity = 0.7,
  86. position = &quot;bottomright&quot;)
  87. )
  88. observeEvent({input$wave_data_slider}, {
  89. data = wave_energy_output()[wave_data2$An_mn_P_OD]
  90. leafletProxy(&quot;wave_data_map&quot;, data = wave_data2) %&gt;%
  91. clearShapes() %&gt;%
  92. addPolygons(data = wave_energy_output,
  93. fillColor = pal,
  94. weight = 0.0,
  95. opacity = 1,
  96. color = &quot;white&quot;,
  97. dashArray = 3,
  98. fillOpacity = 0.7,
  99. ) %&gt;% clearControls() %&gt;%
  100. addLegend(pal = pal,
  101. values = wave_data2$Ave_Depth,
  102. title = &quot;Average depth&quot;,
  103. labFormat = labelFormat (suffix = &quot;m&quot;),
  104. opacity = 0.7,
  105. position = &quot;bottomright&quot;)
  106. }
  107. )}
  108. # Run the application
  109. shinyApp(ui = ui, server = server)

NEW ERROR:

Warning: Error in [: Can't subset columns past the end.
ℹ Locations 43, 57, 57, …, 48, and 46 don't exist.
ℹ There are only 42 columns.

Any help would really be appreciated.

答案1

得分: 1

以下是您提供的代码的翻译部分:

  1. 库(shiny)
  2. 库(leaflet)
  3. 库(rgdal)
  4. 库(sf)
  5. 库(dplyr)
  6. 库(RColorBrewer)
  7. wave_data <- read_sf("~/path/Wave.shp")
  8. wave_data <- st_transform(wave_data, crs = '+proj=longlat +datum=WGS84')
  9. wave_data2 <- wave_data %>%
  10. mutate_at(vars(An_mn_P_OD), ~ as.integer(round(.x)))
  11. ## 加载地图
  12. wave_data_map <- leaflet() %>%
  13. addProviderTiles(providers$Esri.WorldTopoMap) %>%
  14. setView(lng = -4.2026458, lat = 56.4906712, zoom = 5)
  15. wave_data_map
  16. bins <- c(0, 25, 50, 100, 150, 200, 300, 400, 500, 1000, 3100)
  17. pal <- colorBin("RdYlBu", domain = wave_data2$Ave_Depth, bins = bins)
  18. # 定义应用程序的用户界面
  19. ui <- bootstrapPage(
  20. tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  21. leafletOutput("wave_data_map", width = "100%", height = "100%"),
  22. absolutePanel(top = 10, right = 10,
  23. sliderInput("wave_data_range", "Wave energy",
  24. min = 0, max = 75,
  25. value = c(min(wave_data2$An_mn_P_OD),
  26. max(wave_data2$An_mn_P_OD)),
  27. step = 5,
  28. round = 0.5,
  29. dragRange = TRUE)
  30. ))
  31. # 定义服务器逻辑
  32. server <- function(input, output, session) {
  33. wave_energy_output <- reactive({
  34. data <- wave_data2 %>%
  35. filter(An_mn_P_OD >= input$wave_data_range[1],
  36. An_mn_P_OD <= input$wave_data_range[2])
  37. data %>% group_by(Lat)
  38. })
  39. output$wave_data_map <- renderLeaflet({
  40. leaflet() %>%
  41. addProviderTiles(providers$Esri.WorldTopoMap) %>%
  42. setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %>%
  43. addPolygons(data = wave_data2,
  44. weight = 1,
  45. smoothFactor = 0.5,
  46. color = "white",
  47. fillOpacity = 0.3,
  48. fillColor = pal(wave_data2$Ave_Depth)) %>%
  49. addLegend(pal = pal,
  50. values = wave_data2$Ave_Depth,
  51. title = "Average depth",
  52. labFormat = labelFormat (suffix = "m"),
  53. opacity = 0.7,
  54. position = "bottomright")
  55. })
  56. observeEvent(input$wave_data_range, {
  57. data <- wave_energy_output()
  58. leafletProxy("wave_data_map", data = data) %>%
  59. clearShapes() %>%
  60. addPolygons(data = data,
  61. fillColor = pal(wave_energy_output()$Ave_Depth),
  62. weight = 1,
  63. smoothFactor = 0.5,
  64. color = "white",
  65. fillOpacity = 0.3)
  66. })
  67. }
  68. # 运行应用程序
  69. shinyApp(ui = ui, server = server)

希望这有所帮助。如果您有任何其他问题或需要进一步的协助,请随时告诉我。

英文:

Working code:

  1. library(shiny)
  2. library(leaflet)
  3. library(rgdal)
  4. library(sf)
  5. library(dplyr)
  6. library(RColorBrewer)
  7. wave_data &lt;- read_sf(&quot;~/path/Wave.shp&quot;)
  8. wave_data &lt;- st_transform(wave_data, crs = &#39;+proj=longlat
  9. +datum=WGS84&#39;)
  10. wave_data2 &lt;- wave_data %&gt;%
  11. mutate_at(vars(An_mn_P_OD), ~ as.integer(round(.x)))
  12. ## Load map
  13. wave_data_map &lt;- leaflet() %&gt;%
  14. addProviderTiles(providers$Esri.WorldTopoMap) %&gt;%
  15. setView(lng = -4.2026458, lat = 56.4906712, zoom = 5)
  16. wave_data_map
  17. bins &lt;- c(0, 25, 50, 100, 150, 200, 300, 400, 500, 1000, 3100)
  18. pal &lt;- colorBin(&quot;RdYlBu&quot;, domain = wave_data2$Ave_Depth, bins = bins)
  19. # Define UI for application
  20. ui &lt;- bootstrapPage(
  21. tags$style(type = &quot;text/css&quot;, &quot;html, body
  22. {width:100%;height:100%}&quot;),
  23. leafletOutput(&quot;wave_data_map&quot;, width = &quot;100%&quot;, height =
  24. &quot;100%&quot;),
  25. absolutePanel(top = 10, right = 10,
  26. sliderInput(&quot;wave_data_range&quot;, &quot;Wave energy&quot;,
  27. min = 0, max = 75,
  28. value = c(min(wave_data2$An_mn_P_OD),
  29. max(wave_data2$An_mn_P_OD)),
  30. step = 5,
  31. round = 0.5,
  32. dragRange = TRUE)
  33. ))
  34. # Define server logic
  35. server &lt;- function(input, output, session) {
  36. wave_energy_output &lt;- reactive({
  37. data &lt;- wave_data2 %&gt;%
  38. filter(An_mn_P_OD &gt;= input$wave_data_range[1],
  39. An_mn_P_OD &lt;= input$wave_data_range[2])
  40. data %&gt;% group_by(Lat)
  41. })
  42. output$wave_data_map &lt;- renderLeaflet({
  43. leaflet() %&gt;%
  44. addProviderTiles(providers$Esri.WorldTopoMap) %&gt;%
  45. setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %&gt;%
  46. addPolygons(data = wave_data2,
  47. weight = 1,
  48. smoothFactor = 0.5,
  49. color = &quot;white&quot;,
  50. fillOpacity = 0.3,
  51. fillColor = pal(wave_data2$Ave_Depth)) %&gt;%
  52. addLegend(pal = pal,
  53. values = wave_data2$Ave_Depth,
  54. title = &quot;Average depth&quot;,
  55. labFormat = labelFormat (suffix = &quot;m&quot;),
  56. opacity = 0.7,
  57. position = &quot;bottomright&quot;)
  58. })
  59. observeEvent(input$wave_data_range, {
  60. data &lt;- wave_energy_output()
  61. leafletProxy(&quot;wave_data_map&quot;, data = data) %&gt;%
  62. clearShapes() %&gt;%
  63. addPolygons(data = data,
  64. fillColor = pal(wave_energy_output()$Ave_Depth),
  65. weight = 1,
  66. smoothFactor = 0.5,
  67. color = &quot;white&quot;,
  68. fillOpacity = 0.3)
  69. })
  70. }
  71. # Run the application
  72. shinyApp(ui = ui, server = server)

huangapple
  • 本文由 发表于 2023年2月19日 23:39:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501282.html
匿名

发表评论

匿名网友

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

确定