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

如何在Shiny应用中更美观地展示detect_time_anomalies函数输出?

优化Shiny应用中时间异常检测结果的展示方式

当然可以,DT包的datatable组件是替代verbatimTextOutput的绝佳选择,它支持交互筛选、排序、分页,样式美观且可读性更强。以下是修改后的完整代码:

## app.R ##
library(shiny)
library(shinydashboard)
library(bupaR)
library(daqapo)
library(DT)  # 新增:加载DT包

hospital<-structure(list(patient_visit_nr = c(510, 512, 510, 512, 512, 
                                              510, 517, 518, 518, 518, 519, 519, 520, 519, 521, 521, 519, 522, 
                                              522, 523, 524, 525, 526, 523, 527, 527, 527, 528, 528, 523), 
                         activity = c("registration", "Registration", "Triage", "Triage", 
                                      "Clinical exam", "Clinical exam", "Triage", "Registration", 
                                      "Registration", "Registration", "Registration", "Triage", 
                                      "Trage", "Clinical exam", "Triage", "Registration", "Treatment", 
                                      "Registration", "Triaga", "Registration", "Triage", "Triage", 
                                      "Triage", "Triage", "Registration", "Triage", "Clinical exam", 
                                      "Registration", "Triage", "Clinical exam"), originator = c("Clerk 9", 
                                                                                                 "Clerk 12", "Nurse 27", "Nurse 27", "Doctor 7", "Doctor 7", 
                                                                                                 "Nurse 17", "Clerk 12", "Clerk 6", "Clerk 9", "Clerk 12", 
                                                                                                 "Nurse 17", "Nurse 17", "Doctor 4", "Nurse 17", "Clerk 3", 
                                                                                                 "Nurse 17", "Clerk 3", "Nurse 5", "Clerk 9", "Nurse 5", "Nurse 5", 
                                                                                                 "Nurse 5", "Nurse 27", "Clerk 6", "Nurse 5", "Doctor 4", 
                                                                                                 "Nurse 6", "Nurse 6", "Doctor 4"), start = structure(c(1511173097, 
                                                                                                                                                        1511173994, 1511174048, 1511174652, 1511177232, 1511177701, 
                                                                                                                                                        1511264116, 1511264716, 1511264716, 1511264716, 1511265581, 
                                                                                                                                                        1511270783, 1511271796, 1511272354, 1511272924, 1511273120, 
                                                                                                                                                        1511275132, 1511277137, 1511277325, 1511282528, 1511283843, 
                                                                                                                                                        1511283853, 1511283855, 1511285190, 1511287330, 1511287331, 
                                                                                                                                                        1511287333, 1511287817, 1511288105, 1511288209), tzone = "UTC", class = c("POSIXct", 
                                                                                                                                                                                                                                  "POSIXt")), complete = structure(c(1511173206, 1511174220, 
                                                                                                                                                                                                                                                                     1511174508, 1511175017, 1511177637, 1511177769, 1511264340, 
                                                                                                                                                                                                                                                                     1511263336, 1511263336, 1511263336, 1511265952, 1511271138, 
                                                                                                                                                                                                                                                                     1511271540, 1511272954, 1511273169, 1511290800, 1511275761, 
                                                                                                                                                                                                                                                                     1511277499, 1511277484, 1511283119, 1511283965, 1511283968, 
                                                                                                                                                                                                                                                                     1511283970, 1511287261, 1511287447, 1511287448, 1511287450, 
                                                                                                                                                                                                                                                                     1511288104, 1511288412, 1511288528), tzone = "UTC", class = c("POSIXct", 
                                                                                                                                                                                                                                                                                                                                   "POSIXt")), triagecode = c(3, 3, 3, 3, 3, NA, 3, 4, 4, 4, 
                                                                                                                                                                                                                                                                                                                                                              4, 4, 5, 4, 2, 2, 4, 2, 2, 3, 3, 3, 4, 3, 2, 2, 2, 3, 3, 
                                                                                                                                                                                                                                                                                                                                                              3), specialization = c("TRAU", "URG", "TRAU", "URG", "URG", 
                                                                                                                                                                                                                                                                                                                                                                                     "URG", "URG", "PED", "PED", "PED", "PED", "PED", "URG", "PED", 
                                                                                                                                                                                                                                                                                                                                                                                     "URG", "URG", "PED", "URG", "URG", "URG", "URG", "URG", "URG", 
                                                                                                                                                                                                                                                                                                                                                                                     "URG", "URG", "URG", "URG", "TRAU", "TRAU", "URG"), .order = 1:30), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                       -30L), class = c("activitylog", "log", "tbl_df", "tbl", "data.frame"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ), case_id = "patient_visit_nr", activity_id = "activity", resource_id = "originator", timestamps = c("start", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             "complete"))

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    DTOutput("anomaly_table")  # 替换为DTOutput组件
  )
)

server <- function(input, output) {
  
  output$anomaly_table <- renderDT({
    # 生成时间异常检测结果
    anomaly_result <- hospital %>% detect_time_anomalies()
    
    # 将结果转换为数据框(适配datatable)
    anomaly_df <- as.data.frame(anomaly_result)
    
    # 自定义datatable样式与交互
    datatable(
      anomaly_df,
      filter = 'top',  # 顶部添加筛选框
      rownames = FALSE,  # 隐藏行号
      options = list(
        pageLength = 10,  # 默认每页显示10条
        autoWidth = TRUE,  # 自动调整列宽
        scrollX = TRUE  # 横向滚动(适配宽表)
      ),
      colnames = c("病例编号", "活动", "资源", "开始时间", "完成时间", "异常类型", "异常详情")  # 自定义列名(可选)
    ) %>%
      # 为异常行添加高亮样式
      formatStyle(
        columns = c("anomaly_type", "anomaly_message"),
        backgroundColor = styleEqual(c("negative_duration", "start_after_complete"), c("#ffebee", "#ffecb3"))
      )
  })
  
}

shinyApp(ui, server)

关键修改说明:

  • 新增加载DT包,这是实现交互式表格的核心依赖
  • UI端用DTOutput("anomaly_table")替换原有的verbatimTextOutput("print")
  • Server端用renderDT()生成交互式表格:
    • 将detect_time_anomalies()的输出转换为数据框,确保datatable能正常解析
    • 开启顶部筛选、自动列宽、横向滚动等交互功能
    • 自定义列名和异常行的高亮样式,提升可读性

其他可选展示方式:

如果需要更直观的可视化展示异常,还可以结合ggplot2生成时间线异常图,或者用shinyWidgets的表格组件增强样式,但datatable是兼顾美观与实用性的最优选择。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 03:20:39