使用响应式参数函数创建Shiny响应式表格tab.11的技术问询
Got it, let's build out the Shiny implementation you need for that responsive table tab.11. Here's a complete, step-by-step solution tailored to your requirements:
Step 1: Full Shiny App Code
First, here's the complete working code—just make sure your malprof dataset is loaded in your environment before running it:
library(shiny) # Sample malprof dataset (replace with your actual data) # malprof <- read.csv("your_malprof_data.csv") ui <- fluidPage( tabsetPanel( # Target tab panel as specified tabPanel("XI Reports for Sector of Productive Activity", # ASL Selection Dropdown selectInput(inputId = "ASL", label = "Choose an ASL:", choices = c("TOSCANA", "ASL CENTRO", "ASL NORD-OVEST", "ASL SUD-EST"), selected = "TOSCANA"), # Year Selection Dropdown (dynamically populated from dataset) selectInput(inputId = "Anno", label = "Select a Year:", choices = unique(malprof$Year), selected = max(malprof$Year)), # ATECO Level Radio Buttons (matches your "leve..." inputId placeholder) radioButtons(inputId = "level_ateco", label = "Pick ATECO Classification Level:", choices = c("Macro Sector" = "ATECO_Macro", "2-Digit Code" = "ATECO_2cfr"), selected = "ATECO_Macro"), # Responsive Table Output tableOutput("tab.11") ) ) ) server <- function(input, output) { output$tab.11 <- renderTable({ # 1. Filter data based on user's ASL and Year selections filtered_data <- malprof[malprof$ASL == input$ASL & malprof$Year == input$Anno, ] # 2. Select columns to display (includes the chosen ATECO level) # Add additional columns here if you want to include more metrics selected_columns <- c("Year", "ASL", input$level_ateco) result_table <- filtered_data[, selected_columns, drop = FALSE] # Optional: Add grouped summary stats (uncomment if needed) # library(dplyr) # result_table <- filtered_data %>% # group_by(Year, ASL, .data[[input$level_ateco]]) %>% # summarise(Record_Count = n(), .groups = "drop") result_table }, # Enable responsive behavior + table styling responsive = TRUE, striped = TRUE, hover = TRUE, bordered = TRUE ) } shinyApp(ui, server)
Key Details Explained
- Responsive Table: The
responsive = TRUEparameter inrenderTablemakes the table scroll horizontally on small screens, ensuring it's usable across devices. - Dynamic Year Input: The year dropdown uses
unique(malprof$Year)to automatically pull all available years from your dataset—no manual updates needed if your data changes. - ATECO Level Control: The radio buttons map user-friendly labels to your actual dataset column names (
ATECO_Macro/ATECO_2cfr), so users don't need to know the underlying column IDs. - Flexible Filtering: The code filters the dataset to only show rows matching the selected ASL and year, and adjusts the displayed columns based on the ATECO level choice.
- Optional Summary Stats: I included commented-out dplyr code if you want to add grouped counts or other metrics (just uncomment and tweak to your needs).
内容的提问来源于stack exchange,提问作者Gianpaolo Romeo
相关产品推荐
相关产品推荐

