You need to enable JavaScript to run this app. Shiny实现selectInput全选值与多过滤器联动反应式绘图的问题
优惠活动
大模型
产品
解决方案
定价
更多

Shiny实现selectInput全选值与多过滤器联动反应式绘图的问题

Shiny 多过滤器反应式绘图应用修复方案

现存问题定位

  • 过滤流程逻辑颠倒:每次执行单条件过滤就立刻按周期分组聚合,后续过滤操作基于已聚合的中间结果开展,丢失原始业务字段,导致统计结果错乱
  • 全选分支逻辑错误:非全选状态下错误将诊断字段值与诊疗量nb字段做等值匹配,完全偏离筛选规则
  • 多选匹配规则失效:使用==运算符匹配多选输入的多个取值,无法正确筛选符合多选条件的记录
  • 初始状态不符合需求:所有多选输入框默认空值,应用首次加载时无匹配数据,无法实现“默认按月展示全量诊疗量”的要求

优化实现思路

  • 调整执行顺序:先在原始全量数据上叠加所有筛选条件,全部筛选完成后再按所选周期粒度分组求和,避免中途聚合打断过滤流程
  • 完善全选逻辑:对带“Tous”标识的选择项做专门映射,选中全选项时自动匹配对应字段的所有可选值,跳过该字段的过滤限制
  • 统一匹配规则:所有字段匹配使用%in%运算符,兼容单选、多选的取值场景
  • 预置默认选中值:为所有多选输入框设置初始全选值,保证应用首次加载时直接展示全量按月统计的诊疗量数据

可直接运行的修复后代码

library(shiny)
library(ggplot2)
library(dplyr)

# 内置测试数据
data.hosp <- data.frame(stringsAsFactors=FALSE,
                        id= c(1,2,3,4,5),
                        Annee = c(2018, 2018, 2018, 2018, 2018),
                        Mois = c("2018-01","2018-01","2018-02","2018-03","2018-03"),
                        Semaine = c("2018-001","2018-003","2018-008","2018-011","2018-013"),
                        uf= c("A3352","Z6687", "A3352", "A3352", "Z6687"),
                        um= c(3350,6687, 3352, 3350, 6687),
                        ghm= c("AAAAA","DFFDF","DDFDA","AZEEA","DDFDA"),
                        diag= c("A","A","C","Z","R"),
                        nb=c(1,3,1,1,10))

ui <- fluidPage(
  titlePanel("Plot with filters"),
  fluidRow(
    column(2,
           selectInput(inputId = "sel_uf",
                       label = "UF",
                       choices = as.character(unique(data.hosp$uf)), 
                       selected = as.character(unique(data.hosp$uf)),
                       multiple=TRUE,
                       width = validateCssUnit(200))),
    column(2,
           selectInput(inputId = "sel_um", 
                       label = "UM",
                       choices = as.character(unique(data.hosp$um)), 
                       selected = as.character(unique(data.hosp$um)),
                       multiple=TRUE,
                       width = validateCssUnit(200))),
    column(3,
           selectInput(inputId = "sel_ghm", 
                       label = "GHM",
                       choices =as.character(unique(data.hosp$ghm)), 
                       selected = as.character(unique(data.hosp$ghm)),
                       multiple=TRUE,
                       width = validateCssUnit(200))),
    column(3,
           selectInput(inputId = "sel_diag", 
                       label = "Diagnostic",
                       choices =c('Tous', as.character(unique(data.hosp$diag))), 
                       selected = 'Tous',
                       multiple=TRUE,
                       width = validateCssUnit(250))),
    column(4,  selectInput("periodec",
                           label="Affichage par :",
                           choices = c("Semaine"= "Semaine",
                                       "Mois"   = "Mois",
                                       "Année"  = "Annee"),
                           selected   = "Mois" )),
    plotOutput("graph1", height=300 )
  )
)

server <- function(input, output) {
  # 定义周期粒度映射
  periode <- reactive({
    switch(input$periodec,
           "Semaine" = "Semaine",
           "Mois" = "Mois",
           "Année" = "Annee")
  })
  
  # 过滤后反应式数据集
  df_dat <- reactive({
    # 处理诊断字段的全选逻辑
    selected_diag <- if ('Tous' %in% input$sel_diag) {
      unique(data.hosp$diag)
    } else {
      input$sel_diag
    }
    # 全量叠加过滤条件
    df_filtered <- data.hosp %>%
      filter(uf %in% input$sel_uf) %>%
      filter(um %in% input$sel_um) %>%
      filter(ghm %in% input$sel_ghm) %>%
      filter(diag %in% selected_diag)
    # 过滤完成后按周期聚合
    df_filtered %>%
      group_by(.data[[periode()]]) %>%
      summarise(sum_active = sum(nb), .groups = "drop")
  })
  
  # 输出绘图
  output$graph1 <- renderPlot({
    req(df_dat())
    ggplot(df_dat(), aes(x = .data[[periode()]], y = sum_active)) +
      geom_bar(stat = "identity", fill="steelblue")
  }) 
}
shinyApp(ui,server)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.18 10:15:03