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

如何基于三个汇总表的选中内容在Shiny中展示详细数据表?

解决方案

没问题,我来帮你搞定这个需求!要实现点击左侧任意一张汇总表的行,右侧展示对应行的详细数据,核心是要跟踪用户到底选中了哪张表以及对应的行信息。下面是修改后的完整代码,我会在代码后解释关键改动:

library(shiny)
library(DT) 
library(shinydashboard)

ui <- fluidPage(
  dashboardPage(
    dashboardHeader(disable = TRUE),
    dashboardSidebar(disable = TRUE, width = NULL, collapsed = TRUE),
    dashboardBody(
      fixedRow(
        column(4, 
               dataTableOutput("summary_abc"), 
               br(), # 添加换行让表格间隔更清晰
               dataTableOutput("summary_def"), 
               br(),
               dataTableOutput("summary_ghi")),
        column(5, 
               h4("选中行详情"),
               dataTableOutput("employee_details")) # 改用DT展示详情,比文本更直观
      )
    )
  )
)

server <- shinyServer(function(input, output, session) {
  # 把数据集定义在server顶层,方便后续随时访问原始数据
  # 数据集1
  employee <- c('John Doe','Peter Gynn','Jolie Hope')
  salary <- c(21000, 23400, 26800)
  age <- c(45,63,28)
  data1 <- data.frame(employee, salary, age)
  
  # 数据集2
  employee2 <- c('John Doe','Peter Gynn','Jolie Hope')
  qualification <- c("Graduate", "Post Graduate", "Master")
  experience <- c(8,5,17)
  data2 <- data.frame(employee2, qualification, experience)
  colnames(data2)[1] <- "employee" # 统一列名,方便后续识别匹配
  
  # 数据集3
  employee3 <- c('John Doe','Peter Gynn','Jolie Hope', "Jackson")
  weight <- c(60,58,72,59)
  temperature <- c(95,97,96,98)
  data3 <- data.frame(employee3, weight, temperature)
  colnames(data3)[1] <- "employee" # 统一列名
  
  # 创建反应式值,专门存储选中的表格ID和行索引
  selected_info <- reactiveValues(table = NULL, row = NULL)
  
  # 监听第一张表的选中事件
  observeEvent(input$summary_abc_rows_selected, {
    if(!is.null(input$summary_abc_rows_selected)){
      selected_info$table <- "summary_abc"
      selected_info$row <- input$summary_abc_rows_selected
    }
  })
  
  # 监听第二张表的选中事件
  observeEvent(input$summary_def_rows_selected, {
    if(!is.null(input$summary_def_rows_selected)){
      selected_info$table <- "summary_def"
      selected_info$row <- input$summary_def_rows_selected
    }
  })
  
  # 监听第三张表的选中事件
  observeEvent(input$summary_ghi_rows_selected, {
    if(!is.null(input$summary_ghi_rows_selected)){
      selected_info$row <- input$summary_ghi_rows_selected
      selected_info$table <- "summary_ghi"
    }
  })
  
  # 渲染第一张汇总表
  output$summary_abc <- renderDataTable({
    datatable(data1, 
              class = 'cell-border stripe', 
              selection = "single", 
              options = list(ordering=F, dom = 't'), 
              caption = "Summary 1", 
              rownames = FALSE)
  })
  
  # 渲染第二张汇总表
  output$summary_def <- renderDataTable({
    datatable(data2, 
              class = 'cell-border stripe', 
              selection = "single", 
              options = list(ordering=F, dom = 't'), 
              caption = "Summary 2", 
              rownames = FALSE)
  })
  
  # 渲染第三张汇总表
  output$summary_ghi <- renderDataTable({
    datatable(data3, 
              class = 'cell-border stripe', 
              selection = "single", 
              options = list(ordering=F, dom = 't'), 
              caption = "Summary 3", 
              rownames = FALSE)
  })
  
  # 渲染选中行的详情
  output$employee_details <- renderDataTable({
    req(selected_info$table, selected_info$row) # 确保有选中信息后再渲染
    
    # 根据选中的表格ID,匹配对应的数据集并提取选中行
    selected_data <- switch(selected_info$table,
                            "summary_abc" = data1[selected_info$row, , drop = FALSE],
                            "summary_def" = data2[selected_info$row, , drop = FALSE],
                            "summary_ghi" = data3[selected_info$row, , drop = FALSE])
    
    # 渲染详情表格,去掉多余功能,只展示选中行
    datatable(selected_data, 
              class = 'cell-border stripe',
              options = list(ordering=F, dom = 't', searching = FALSE),
              rownames = FALSE,
              caption = paste("来自", gsub("summary_", "", selected_info$table), "的选中行详情"))
  })
})

shinyApp(ui = ui, server = server)

关键改动说明

  • 数据集全局化:把三个dataframe移到server函数顶层,这样后续渲染详情时能直接访问原始数据,不用每次渲染表格都重新生成数据。
  • 反应式值跟踪选中状态:创建selected_info这个reactiveValues对象,专门存储用户当前选中的表格ID和行索引,这是实现跨表格选中跟踪的核心。
  • 监听单表选中事件:为每个表格单独添加observeEvent,监听对应的input$tableId_rows_selected事件,一旦用户选中行就更新selected_info的值。
  • 动态渲染详情内容:用req()确保有有效选中信息后,通过switch()函数匹配对应数据集,提取选中行并渲染成详情表格,比原有的文本输出更直观。
  • 细节优化:统一了三个数据集的员工列名,添加换行让左侧表格布局更美观,详情表标题会自动显示来自哪张汇总表。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 07:59:42