首页 > 解决方案 > 如何在闪亮的应用程序中将反应参数同步到光栅砖

问题描述

我正在尝试使用 16 层的光栅砖创建一个闪亮的应用程序。我似乎无法梳理出服务器中的反应参数。似乎无论我尝试什么,我都无法让 selectInput 与我对栅格砖名称的反应输入同步。为什么我的闪亮应用程序没有拾取单独的图层?selectInput 的值与栅格砖的名称相匹配。我尝试了不同的方法让反应函数获取层的名称。

更新:我发现这个旧帖子显示必须为每一层指定数据路径。有人可以更详细地解释一下为每一层指定数据路径吗?

https://gis.stackexchange.com/questions/274395/using-r-shiny-to-create-a-plot-with-a-shapefile-and-a-raster

library(leafem)
library(terra)
library(raster)
library(sp)
library(here)
library(stars)
library(lubridate)
library(tidyverse)
library(rasterVis)
library(shiny)
library(leaflet)
library(patchwork)
library(plotly)
library(rgdal)
library(terra)
library(rasterVis)
library(animation)
library(htmlwidgets)


url <- "http://thredds.cdip.ucsd.edu//thredds/fileServer/cdip/model/MOP_grids/CA_0.01_forecast.nc"

options(timeout = 10000)

data <- download.file(url, "/Users/mycomp/Desktop/wave_data.nc")

data_set <- "wave_data.nc"


waves <- rast(data_set, sub = "waveHs")
ROU <- ext(-121.0062, -118.7438, 33.10625, 34.90625)
wave_crop <- crop(waves, ROU)

remove_txt <- gsub("waveHs_waveTime.", "", names(wave_crop)) %>% 
  as.numeric() %>% 
  as_datetime()

names(wave_crop) <- c(remove_txt)

names(wave_crop)



raz_temp <- rast(xmin = -121.0062,
                      xmax = -118.7438,
                      ymin = 33.10625,
                      ymax = 34.90625,
                      resolution = c(0.001, 0.001))

brick_resample <- resample(wave_crop, raz_temp)

names(brick_resample)


wave_pal <- colorRampPalette(c("blue", "cyan", "yellow", "red"))



library(shiny)
ui <- fluidPage(
  
  titlePanel("Wave Model"),
  
  leafletOutput("brick_resample", height = 500),
  
  selectInput("names", "Select Date Range",
              names(brick_resample))
  
)

server <- function(input, output, session){
  
  #Reactive
   slid <- reactive({
     raster::brick(brick_resample$layer$datapath)
     #brick_resample$names
     #names(brick_resample)
     #brick_resample[names]
   })


  #Map
  output$brick_resample <- renderLeaflet({
    
    leaflet(options = leafletOptions(minZoom = 8.5)) %>% 
      addPolygons(data = islands, color = 'black', opacity = 1, weight = 2, fill = F)%>% 
      addPolygons(data = ca, color = 'black', opacity = 1, weight = 2, fill = F)%>%
      addProviderTiles("Esri.OceanBasemap") %>% 
      addRasterImage(x = slid(), colors = wave_pal(5)) %>% 
        setView(lng = -119.200336, lat = 34.14, zoom = 9) %>% 
        setMaxBounds(lng1 = -121.0,
                 lat1 = 33.1125,
                 lng2 = -118.75,
                 lat2 = 34.9)
    })
}

shinyApp(ui = ui, server = server)
  

标签: rshinyraster

解决方案


推荐阅读