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
- Case Mismatch in Dataset Selection: Your sidebar's
selectInputuses"Local government"(lowercase 'g'), but theswitchstatement 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. - 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). - 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(ornew, as you referenced in your question) andcentralinto your R environment before running the app—for example, usinglocal <- read.csv("local_data.csv")andcentral <- read.csv("central_data.csv"). - Column Name Consistency: Ensure both datasets have matching column names (
name,type,servicein 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
相关产品推荐
相关产品推荐

