英文:
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.
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 <- 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)))
## Load map
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)
## Add polygons
wave_data_map <- 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.5,
fillColor = pal(wave_data2$Ave_Depth),
)
wave_data_map
## Add legend
wave_data_map <- 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")
wave_data_map
# Define UI for application
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)
))
# Define server logic
server <- function(input, output, session) {
wave_energy_output <- reactive ({
wave_data2 %>%
filter(An_mn_P_OD >= input$wave_data_range[1]) %>%
filter(An_mn_P_OD <= input$wave_data_range[2]) %>%
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_slider}, {
data = wave_energy_output()[wave_data2$An_mn_P_OD]
leafletProxy("wave_data_map", data = wave_data2) %>%
clearShapes() %>%
addPolygons(data = wave_energy_output,
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")
}
)}
# 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 <- 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)))
## Load map
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)
# Define UI for application
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)
))
# Define server logic
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)
})
}
# Run the application
shinyApp(ui = ui, server = server)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论