【发布时间】:2022-06-19 23:15:47
【问题描述】:
我有一个应用程序,它应该显示某个地区的作物类型的地图。 数据是我要在选择地区后加载的几何数据(.shp 文件)。 数据是通过 st_read() 加载为 sf 对象还是从工作区加载,我不介意。
用户应该通过selectInput在第一个选项卡中选择地区,然后我希望加载数据,以便在第二个选项卡的地图上显示该地区。在这里,用户应该能够进一步从区域中选择一个区域(“Landkreis”)以及要显示的作物种类(“Kultur”)。
我需要在做出选择后加载数据,因为数据太大而无法一次加载所有数据。
现在的问题是数据没有加载,但我没有收到任何错误消息。仅显示基本地图,第二个选项卡上的 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)
【问题讨论】: