You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shiny切换数据集功能失效:仅加载单个数据集问题求助

Fixing Dataset Switching Issue in Your Shiny App

Let's walk through the problems in your code and fix them step by step:

Key Issues Identified

  1. Case Mismatch in Dataset Selection: Your sidebar's selectInput uses "Local government" (lowercase 'g'), but the switch statement in the server looks for "Local Government" (uppercase 'G'). This mismatch means selecting the local dataset never triggers a switch, so your app defaults to showing the central dataset.
  2. Incorrect Choices for Sidebar Filters: You're using names(datasetInput()) for your filter dropdowns, which returns the column names of your dataset—not the actual values within those columns (like department names or service types).
  3. Missing Input Validation: Without checking if the dataset is loaded before rendering UI elements, you might run into errors when switching datasets.

Corrected Code

Here's the updated version of your app with fixes applied:

UI Part

# Define UI for application
fluidPage(
  titlePanel("Public services"),
  sidebarLayout(
    sidebarPanel(
      # Fix: Match case with the server's switch statement
      selectInput("dataset", "Choose dataset:", choices = c("Local Government", "Central Government")),
      uiOutput('name'),
      uiOutput('type'),
      uiOutput('service')
    ),
    mainPanel(
      h4("Summary"),
      verbatimTextOutput("summary"),
      h4("Observations"),
      DT::dataTableOutput("table")
    )
  )
)

Server Part

# Define server logic
function(input, output) {
  # Reactive expression to return the selected dataset
  datasetInput <- reactive({
    # Ensure your datasets 'local' (or 'new' as you mentioned) and 'central' are loaded first!
    switch(input$dataset,
           "Central Government" = central,
           "Local Government" = local)
  })
  
  # Filter dropdowns - now using actual column values instead of column names
  output$name = renderUI({
    req(datasetInput())  # Wait until dataset is loaded to avoid errors
    selectInput('name', h5('Department or Local authority name'), 
                choices = unique(datasetInput()$name))  # Get unique values from 'name' column
  })
  
  output$type = renderUI({
    req(datasetInput())
    selectInput('type', h5('Service type'), 
                choices = unique(datasetInput()$type))  # Get unique values from 'type' column
  })
  
  output$service = renderUI({
    req(datasetInput())
    selectInput('service', h5('Service'), 
                choices = unique(datasetInput()$service))  # Get unique values from 'service' column
  })
  
  # Render data table
  output$table <- DT::renderDataTable({
    datasetInput()
  })
  
  # Render summary (added back since it was missing in your server code)
  output$summary <- renderPrint({
    req(datasetInput())
    summary(datasetInput())
  })
}

Additional Notes

  • Load Your Datasets: Make sure you load local (or new, as you referenced in your question) and central into your R environment before running the app—for example, using local <- read.csv("local_data.csv") and central <- read.csv("central_data.csv").
  • Column Name Consistency: Ensure both datasets have matching column names (name, type, service in this case) so the dropdowns work seamlessly when switching datasets. If column names differ, you'll need to adjust the code to map them appropriately.

内容的提问来源于stack exchange,提问作者Inga Sileryte

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:35:40