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

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

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

问题

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

我正在尝试使用滑块功能在Leaflet地图上使用Shiny来选择并仅显示选定的多边形。

以下是我的代码:

library(shiny)
library(leaflet)
library(rgdal)
library(sf)
library(dplyr)
library(RColorBrewer)

# 读取波浪数据
wave_data <- read_sf("~/path/Wave.shp")

# 转换坐标系
wave_data <- st_transform(wave_data, crs = '+proj=longlat +datum=WGS84')

# 处理数据
wave_data2 <- wave_data %>%
  mutate_at(vars(An_mn_P_OD), ~ as.integer(round(.x)))

# 创建Leaflet地图
wave_data_map <- leaflet() %>%
  addProviderTiles(providers$Esri.WorldTopoMap) %>%
  setView(lng = -4.2026458, lat = 56.4906712, zoom = 5)

# 设置深度分级
bins <- c(0, 25, 50, 100, 150, 200, 300, 400, 500, 1000, 3100)
pal <- colorBin("RdYlBu", domain = wave_data2$Ave_Depth, bins = bins)

# 添加多边形
wave_data_map <- wave_data_map %>%
  addPolygons(data = wave_data2, 
              weight = 1,
              smoothFactor = 0.5,
              color = "white", 
              fillOpacity = 0.5, 
              fillColor = pal(wave_data2$Ave_Depth))

# 添加图例
wave_data_map <- wave_data_map %>%
  addLegend(pal = pal, 
            values = wave_data2$Ave_Depth, 
            title = "Average depth",
            labFormat = labelFormat(suffix = "m"),
            opacity = 0.7, 
            position = "bottomright")

# 定义UI
ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("wave_data_map", width = "100%", height = "100%"),
  sliderInput("wave_data_slider", "Wave energy", 
              min = 0, max = 75, 
              value = c(min(wave_data2$An_mn_P_OD), max(wave_data2$An_mn_P_OD)), 
              step = 5, 
              round = 0.5,
              dragRange = TRUE)
)

# 定义服务器逻辑
server <- function(input, output, session) {
  # 创建反应式对象
  wave_energy_output <- reactive({
    wave_data2 %>%
      filter(An_mn_P_OD >= input$wave_data_slider[1]) %>%
      filter(An_mn_P_OD <= input$wave_data_slider[2]) %>%
      group_by(Lat)
  })
  
  # 渲染Leaflet地图
  output$wave_data_map <- renderLeaflet({
    leaflet() %>%
      addProviderTiles(providers$Esri.WorldTopoMap) %>%
      setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %>%
      addPolygons(data = wave_data2, 
                  weight = 1,
                  smoothFactor = 0.5,
                  color = "white", 
                  fillOpacity = 0.3, 
                  fillColor = pal(wave_data2$Ave_Depth)) %>%
      addLegend(pal = pal, 
                values = wave_data2$Ave_Depth, 
                title = "Average depth",
                labFormat = labelFormat(suffix = "m"),
                opacity = 0.7, 
                position = "bottomright")
  })
  
  observeEvent({input$wave_data_slider}, {
    data <- wave_energy_output()[wave_data2$An_mn_P_OD]
    leafletProxy("wave_data_map", data = wave_data2) %>%
      clearShapes() %>%
      addPolygons(data = data,
                  fillColor = pal, 
                  weight = 0.0, 
                  opacity = 1, 
                  color = "white", 
                  dashArray = 3, 
                  fillOpacity = 0.7, 
      ) %>%
      clearControls() %>%
      addLegend(pal = pal, 
                values = wave_data2$Ave_Depth, 
                title = "Average depth",
                labFormat = labelFormat(suffix = "m"),
                opacity = 0.7, 
                position = "bottomright")
  })
}

# 运行应用
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:

library(shiny)
library(leaflet)
library(rgdal)
library(sf)
library(dplyr)
library(RColorBrewer)
wave_data &lt;- read_sf(&quot;~/path/Wave.shp&quot;)
wave_data &lt;- st_transform(wave_data, crs = &#39;+proj=longlat 
+datum=WGS84&#39;)
wave_data2 &lt;- wave_data %&gt;% 
mutate_at(vars(An_mn_P_OD), ~ as.integer(round(.x))) 
## Load map
wave_data_map &lt;- leaflet() %&gt;% 
addProviderTiles(providers$Esri.WorldTopoMap) %&gt;% 
setView(lng = -4.2026458, lat = 56.4906712, zoom = 5)
wave_data_map
bins &lt;- c(0, 25, 50, 100, 150, 200, 300, 400, 500, 1000, 3100)
pal &lt;- colorBin(&quot;RdYlBu&quot;, domain = wave_data2$Ave_Depth, bins = 
bins)
## Add polygons
wave_data_map &lt;- leaflet() %&gt;% 
addProviderTiles(providers$Esri.WorldTopoMap) %&gt;% 
setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %&gt;% 
addPolygons(data = wave_data2, 
weight = 1,
smoothFactor = 0.5,
color = &quot;white&quot;, 
fillOpacity = 0.5, 
fillColor = pal(wave_data2$Ave_Depth), 
)
wave_data_map
## Add legend
wave_data_map &lt;- leaflet() %&gt;% 
addProviderTiles(providers$Esri.WorldTopoMap) %&gt;% 
setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %&gt;% 
addPolygons(data = wave_data2, 
weight = 1,
smoothFactor = 0.5,
color = &quot;white&quot;, 
fillOpacity = 0.3, 
fillColor = pal(wave_data2$Ave_Depth)) %&gt;% 
addLegend(pal = pal, 
values = wave_data2$Ave_Depth, 
title = &quot;Average depth&quot;,
labFormat = labelFormat (suffix = &quot;m&quot;),
opacity = 0.7, 
position = &quot;bottomright&quot;)
wave_data_map
# Define UI for application
ui &lt;- bootstrapPage(
tags$style(type = &quot;text/css&quot;, &quot;html, body 
{width:100%;height:100%}&quot;),
leafletOutput(&quot;wave_data_map&quot;, width = &quot;100%&quot;, height = 
&quot;100%&quot;),
sliderInput(&quot;wave_data_slider&quot;, &quot;Wave energy&quot;, 
min = 0, max = 75, 
value = c(min(wave_data2$An_mn_P_OD), 
max(wave_data2$An_mn_P_OD)), 
step = 5, 
round = 0.5,
dragRange = TRUE)
))
# Define server logic
server &lt;- function(input, output, session) {
wave_energy_output &lt;- reactive ({
wave_data2 %&gt;% 
filter(An_mn_P_OD &gt;= input$wave_data_range[1]) %&gt;% 
filter(An_mn_P_OD &lt;= input$wave_data_range[2]) %&gt;% 
group_by(Lat)
})
output$wave_data_map &lt;- renderLeaflet(
leaflet() %&gt;% 
addProviderTiles(providers$Esri.WorldTopoMap) %&gt;% 
setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %&gt;% 
addPolygons(data = wave_data2, 
weight = 1,
smoothFactor = 0.5,
color = &quot;white&quot;, 
fillOpacity = 0.3, 
fillColor = pal(wave_data2$Ave_Depth)) %&gt;% 
addLegend(pal = pal, 
values = wave_data2$Ave_Depth, 
title = &quot;Average depth&quot;,
labFormat = labelFormat (suffix = &quot;m&quot;),
opacity = 0.7, 
position = &quot;bottomright&quot;)
)
observeEvent({input$wave_data_slider}, {
data = wave_energy_output()[wave_data2$An_mn_P_OD]
leafletProxy(&quot;wave_data_map&quot;, data = wave_data2) %&gt;% 
clearShapes() %&gt;% 
addPolygons(data = wave_energy_output,
fillColor = pal, 
weight = 0.0, 
opacity = 1, 
color = &quot;white&quot;, 
dashArray = 3, 
fillOpacity = 0.7, 
) %&gt;% clearControls() %&gt;% 
addLegend(pal = pal, 
values = wave_data2$Ave_Depth, 
title = &quot;Average depth&quot;,
labFormat = labelFormat (suffix = &quot;m&quot;),
opacity = 0.7, 
position = &quot;bottomright&quot;)
}
)}
# Run the application 
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

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

库(shiny)
库(leaflet)
库(rgdal)
库(sf)
库(dplyr)
库(RColorBrewer)

wave_data <- read_sf("~/path/Wave.shp")

wave_data <- st_transform(wave_data, crs = '+proj=longlat +datum=WGS84')

wave_data2 <- wave_data %>%
   mutate_at(vars(An_mn_P_OD), ~ as.integer(round(.x))) 

## 加载地图

wave_data_map <- leaflet() %>%
   addProviderTiles(providers$Esri.WorldTopoMap) %>%
   setView(lng = -4.2026458, lat = 56.4906712, zoom = 5)
wave_data_map

bins <- c(0, 25, 50, 100, 150, 200, 300, 400, 500, 1000, 3100)
pal <- colorBin("RdYlBu", domain = wave_data2$Ave_Depth, bins = bins)

# 定义应用程序的用户界面

ui <- bootstrapPage(
   tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
   leafletOutput("wave_data_map", width = "100%", height = "100%"),
   absolutePanel(top = 10, right = 10, 
   sliderInput("wave_data_range", "Wave energy", 
           min = 0, max = 75, 
           value = c(min(wave_data2$An_mn_P_OD), 
                     max(wave_data2$An_mn_P_OD)), 
           step = 5, 
           round = 0.5,
           dragRange = TRUE)
))

# 定义服务器逻辑

server <- function(input, output, session) {
   wave_energy_output <- reactive({
      data <- wave_data2 %>% 
         filter(An_mn_P_OD >= input$wave_data_range[1],
            An_mn_P_OD <= input$wave_data_range[2])
      data %>% group_by(Lat)
   })

  output$wave_data_map <- renderLeaflet({
   leaflet() %>% 
     addProviderTiles(providers$Esri.WorldTopoMap) %>% 
     setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %>% 
     addPolygons(data = wave_data2, 
                 weight = 1,
                 smoothFactor = 0.5,
                 color = "white", 
                 fillOpacity = 0.3, 
                 fillColor = pal(wave_data2$Ave_Depth)) %>% 
     addLegend(pal = pal, 
               values = wave_data2$Ave_Depth, 
               title = "Average depth",
               labFormat = labelFormat (suffix = "m"),
               opacity = 0.7, 
               position = "bottomright")
 })

 observeEvent(input$wave_data_range, {
    data <- wave_energy_output()
    leafletProxy("wave_data_map", data = data) %>% 
     clearShapes() %>% 
     addPolygons(data = data,
                 fillColor = pal(wave_energy_output()$Ave_Depth), 
                 weight = 1,
                 smoothFactor = 0.5,
                 color = "white", 
                 fillOpacity = 0.3)
   })

}

# 运行应用程序

shinyApp(ui = ui, server = server)

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

英文:

Working code:

library(shiny)
library(leaflet)
library(rgdal)
library(sf)
library(dplyr)
library(RColorBrewer)
wave_data &lt;- read_sf(&quot;~/path/Wave.shp&quot;)
wave_data &lt;- st_transform(wave_data, crs = &#39;+proj=longlat 
+datum=WGS84&#39;)
wave_data2 &lt;- wave_data %&gt;% 
mutate_at(vars(An_mn_P_OD), ~ as.integer(round(.x))) 
## Load map
wave_data_map &lt;- leaflet() %&gt;% 
addProviderTiles(providers$Esri.WorldTopoMap) %&gt;% 
setView(lng = -4.2026458, lat = 56.4906712, zoom = 5)
wave_data_map
bins &lt;- c(0, 25, 50, 100, 150, 200, 300, 400, 500, 1000, 3100)
pal &lt;- colorBin(&quot;RdYlBu&quot;, domain = wave_data2$Ave_Depth, bins = bins)
# Define UI for application
ui &lt;- bootstrapPage(
tags$style(type = &quot;text/css&quot;, &quot;html, body 
{width:100%;height:100%}&quot;),
leafletOutput(&quot;wave_data_map&quot;, width = &quot;100%&quot;, height = 
&quot;100%&quot;),
absolutePanel(top = 10, right = 10, 
sliderInput(&quot;wave_data_range&quot;, &quot;Wave energy&quot;, 
min = 0, max = 75, 
value = c(min(wave_data2$An_mn_P_OD), 
max(wave_data2$An_mn_P_OD)), 
step = 5, 
round = 0.5,
dragRange = TRUE)
))
# Define server logic
server &lt;- function(input, output, session) {
wave_energy_output &lt;- reactive({
data &lt;- wave_data2 %&gt;% 
filter(An_mn_P_OD &gt;= input$wave_data_range[1],
An_mn_P_OD &lt;= input$wave_data_range[2])
data %&gt;% group_by(Lat)
})
output$wave_data_map &lt;- renderLeaflet({
leaflet() %&gt;% 
addProviderTiles(providers$Esri.WorldTopoMap) %&gt;% 
setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %&gt;% 
addPolygons(data = wave_data2, 
weight = 1,
smoothFactor = 0.5,
color = &quot;white&quot;, 
fillOpacity = 0.3, 
fillColor = pal(wave_data2$Ave_Depth)) %&gt;% 
addLegend(pal = pal, 
values = wave_data2$Ave_Depth, 
title = &quot;Average depth&quot;,
labFormat = labelFormat (suffix = &quot;m&quot;),
opacity = 0.7, 
position = &quot;bottomright&quot;)
})
observeEvent(input$wave_data_range, {
data &lt;- wave_energy_output()
leafletProxy(&quot;wave_data_map&quot;, data = data) %&gt;% 
clearShapes() %&gt;% 
addPolygons(data = data,
fillColor = pal(wave_energy_output()$Ave_Depth), 
weight = 1,
smoothFactor = 0.5,
color = &quot;white&quot;, 
fillOpacity = 0.3)
})
}
# Run the application 
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:

确定