在 R shiny 2 中使用 sliderInput 动态渲染等值线图

问题描述 投票:0回答:0

我正在尝试使用滑块功能在 Leaflet 地图上使用 Shiny 选择多边形。

以下帖子将我引向正确的答案,但我看不到 sliderInput 如何调用正确的多边形。我缺少一些东西。

在 R shiny 中使用 sliderInput 动态渲染等值线图

数据为wave-shp (wave.shp) 如下页面:https://www.renewables-atlas.info/downloads/。使用 QGIS 转换为 geoJSON。

我怀疑我需要在某个地方过滤,但不确定这应该在 ui 还是服务器中,也不确定如何将过滤器与滑块功能相关联。

这是我的代码:

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

wave_data <-   read_sf(Wave_data_32631.geojson")

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

## 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_data$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_data, 
           weight = 1,
           smoothFactor = 0.5,
           color = "white", 
           fillOpacity = 0.5, 
           fillColor = pal(wave_data$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_data, 
           weight = 1,
           smoothFactor = 0.5,
           color = "white", 
           fillOpacity = 0.3, 
           fillColor = pal(wave_data$Ave_Depth)) %>% 
   addLegend(pal = pal, 
         values = wave_data$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%"),
   absolutePanel(top = 10, right = 10, 
             sliderInput("wave_data_range", "Wave energy", 
                         min = 0, max = 75, 
                         value = range(wave_data$An_mn_P_OD), step = 5, 
                         dragRange = TRUE)

))

# Define server logic

server <- function(input, output) {
  output$categoryOut <- renderUI({
     sliderInput("wave_data_range", "Wave energy", 
                 min = 0, max = 75, 
                 value = range(wave_data$An_mn_P_OD), step = 5, 
                 dragRange = TRUE)
  })
output$wave_data_map <- renderLeaflet({
  leaflet(wave_data_map) %>% 
     addProviderTiles(providers$Esri.WorldTopoMap) %>% 
     setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %>% 
     addPolygons(data = wave_data, 
                 weight = 1,
                 smoothFactor = 0.5,
                 color = "white", 
                 fillOpacity = 0.3, 
                 fillColor = pal(wave_data$Ave_Depth)) %>% 
     addLegend(pal = pal, 
               values = wave_data$Ave_Depth, 
               title = "Average depth",
               labFormat = labelFormat (suffix = "m"),
               opacity = 0.7, 
               position = "bottomright")
})

观察({ 输入$wave_data_range leafletProxy("wave_data_map") %>% setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) }) }

# Run the application 

shinyApp(ui = ui, server = server)

我试过'observe'和'observeEvent',但我对这些不熟悉。 ? observeEvent() 返回 bindEvent() 现在应该使用,但又不熟悉。

shiny qgis shinyapps r-leaflet
© www.soinside.com 2019 - 2024. All rights reserved.