簡體   English   中英

根據 selectInput 將數據加載到 R shiny 應用程序中

[英]Load data into a R shiny app depending on selectInput

我有一個應用程序,它應該顯示某個地區作物類型的 map。 數據是我想在選擇地區后加載的幾何數據(.shp 文件)。 數據是通過 st_read() 加載為 sf 對象還是從工作區加載,我不介意。

用戶應該通過selectInput在第一個選項卡中選擇地區,然后我要加載數據,以便該地區顯示在第二個選項卡中的map上。 在這里,用戶應該能夠進一步從地區中選擇一個地區(“Landkreis”)和要顯示的作物種類(“Kultur”)。
我需要在做出選擇后加載數據,因為數據太大而無法一次加載所有數據。

現在的問題是數據尚未加載,但我沒有收到任何錯誤消息。 僅顯示基本 map,第二個選項卡上的 selectInput 菜單為空。

任何幫助,將不勝感激。

這是一個(希望如此)可重現的例子(沒有數據):

library(shinydashboard)
library(leaflet)
library(tidyverse)
library(sf)

ui <- dashboardPage(
  dashboardHeader(title = "LAWA",titleWidth = 200),
  dashboardSidebar(width = 200,            
                   sidebarMenu(id = "sidebarmenu", style = "position: Scroll; overflow: visible",  
                               
                               menuItem("choose file ", tabName = "choice",icon = icon("wrench")),
                               menuItem("map", tabName = "map",icon = icon("envira")), 
                               
                               conditionalPanel(condition = 'input.sidebarmenu == "map"',
                                                div(id = "form",
                                                    tags$hr(), 
                                                    selectInput(inputId = "gewLandkreis1", label = "Landkreise", choices = NULL), 
                                                    selectInput(inputId = "Kultur1", label = "Kultur",choices = NULL) 
                                                )            
                               )
                   )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "choice",
              selectInput(inputId = "gewRBZ1", label = "Please choose a district", choices = c("Mittelfranken","Niederbayern","Oberbayern","Oberfranken","Oberpfalz","Schwaben","Unterfranken"), selected = "Mittelfranken"), 
      ),
      
      tabItem(tabName = "map",
              fluidRow(style = "background-color:#D3D3D3;",column(12,h3(textOutput(outputId = "RBZ_name"))),
              ),
              tags$br(),
               column(12,
                     box(title = "district map",solidHeader = T, width = 14,status = "primary",
                         leafletOutput("map1", width = "1050px", height = "750px"))),
              
      ) #  Tabitem
    ) #  tabItems
  ) #  DashboardBody
) #  ui

server <- function(input, output, session){
  
  Inv <- reactive({    # here I want to load the data depending on the district chosen. the districts name is in every file, e.g. Inv_2018_Oberbayern.Rdata
    req(input$RBZ1)   
    name <- gsub(" ","",paste("Inv_2018_",input$RBZ1,".shp"))  #  name of the file  
    data <- st_read(dsn = name) # load data from file as sf object with st_read
  })
  
  # include district name in heading
  output$RBZ_name<-renderText({
    req(input$RBZ1)
    paste0("land use and crop growth in: ",input$gewRBZ1)
    
  })
  
  # observe function for region (= Landkreis) depending on district chosen
  observe({
    req(input$RBZ1)
    choice_LK <-  unique(Inv()$`BEZ_KRS`) 
    updateSelectInput(session, "gewLandkreis1", "Landkreise", choices = sort(choice_LK))
    
  })
  
  # observe function for crop type (= Kategorie) depending on district chosen
  observe({
    req(input$RBZ1)
    choice_Kultur <- sort(unique(Inv()$`Kategorie`))
    updateSelectInput(session, "Kultur1", "Kultur", choices = choice_Kultur)
  })
  
  # fiter data depending on chosen region and crop type for map
  data_input <- reactive({
    Inv() %>%
      filter(BEZ_KRS == input$gewLandkreis1) %>%
      filter(Kategorie  == input$Kultur1)
  })
  
  # popup definition
  # map popup for crops
  mappopup_Kultur <- reactive({
    paste(sep = "<br/>",
          paste0("<i>Fruchtart: <i>", data_input()$`Art`),
          paste0("<i>Fläche [ha] <i>", data_input()$`flaeche`),
          paste0("<i>Code: <i>", data_input()$`Code`),
          paste0("<i>Gemeinde: <i>", data_input()$`BEZ_GEM`))
  })
  
  # make map1 with leaflet
  output$map1 <- renderLeaflet({
    
    # base map
    map1 <- leaflet() %>%
      addTiles(group = "street map") %>%
      addProviderTiles(provider = providers$OpenTopoMap, group = "topo map")
    
  })
  
  # observe function for crop type and region
  observe({
    
    factpal <- colorFactor("RdYlGn", data_input()$`Art`)
    
    leafletProxy("map1") %>%
      clearControls() %>%
      clearShapes() %>%
      setView(lng = mean(st_bbox(data_input())[c(1,3)]), lat = mean(st_bbox(data_input())[c(2,4)]), zoom = 11) %>%
      addPolygons(data = data_input(), layerId = data_input()$`Code`, color =  ~factpal(Art), opacity = 0.8,
                  popup = mappopup_Kultur()) %>%
      addLegend("bottomright", pal = factpal, values = data_input()$`Art`) %>%
      addLayersControl(baseGroups = c("street map", "topo map"),
                       options = layersControlOptions(collapsed = F)) 
    
  })
}
  
  shinyApp(ui = ui, server = server)

有沒有可能是你需要一些不存在的東西? 見下文,如果您將input$RBZ1更改為input$gewRBZ1它應該加載您的文件。 甜蜜的應用程序,順便說一句!


  Inv <- reactive({    # here I want to load the data depending on the district chosen. the districts name is in every file, e.g. Inv_2018_Oberbayern.Rdata
    req(input$RBZ1) #should it be 'input$gewRBZ1'
    name <- gsub(" ","",paste("Inv_2018_",input$RBZ1,".shp"))  #  name of the file  
    data <- st_read(dsn = name) # load data from file as sf object with st_read
  })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM