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

Shiny应用中DT::datatable()无法展示二维数据框的问题

问题解决:Shiny中DT表格展示报错的修复

错误根源

UI部分直接调用datatable("dt")是错误写法——该操作是把字符串"dt"作为数据传入DT的datatable()函数,但该函数要求输入二维数据(如数据框、矩阵),因此触发报错。正确做法是用dataTableOutput()承接server端生成的表格输出。

修正后的完整代码

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

initial<-structure(list(case_id = c("3397364", "3397364"), action = c("3397364-RAAMELK", 
                                                                     "3397364-RAAMELK"), resource = c("RAAMELK", "RAAMELK"), lifecycle = c(1, 
                                                                                                                                           1), registration_type = structure(1:2, .Label = c("start", "complete"
                                                                                                                                           ), class = "factor"), timestamp = structure(c(1667523600, 1667531220
                                                                                                                                           ), tzone = "UTC", class = c("POSIXct", "POSIXt")), activity = c("RAAMELK", 
                                                                                                                                                                                                           "RAAMELK"), activity_description = c("Forbrukt r<e5>melk", "Forbrukt r<e5>melk"
                                                                                                                                                                                                           ), ...9 = c(NA, NA), product = c("K101152", "K101152"), product_type_text = c("200100 - Milk", 
                                                                                                                                                                                                                                                                                         "200100 - Milk"), qty = c(NA, 31), in_out = c("in", "out"), qty_scrap = c(NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                 NA_real_), `FP ordre` = c(NA_character_, NA_character_), Artikkeltype = c("SF", 
                                                                                                                                                                                                                                                                                                                                                                                                                                           "SF"), .order = 1:2), row.names = c(NA, -2L), class = c("eventlog", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   "log", "tbl_df", "tbl", "data.frame"), case_id = "case_id", activity_id = "activity", activity_instance_id = "action", lifecycle_id = "registration_type", resource_id = "resource", timestamp = "timestamp")
shinyApp(
  ui = dashboardPage(
    dashboardHeader(),
    dashboardSidebar(),
    dashboardBody(
      # 修正:用dataTableOutput承接server端输出
      dataTableOutput("dt")
      #selectInput("cols","Select column to convert",choices = colnames(initial),multiple = F,selected =colnames(initial)[1] ),
      #actionButton("conv","Convert to numeric")
    )
  ),
  server = function(input, output, session) { # add session so we can update
    output$dt<-renderDataTable({
      
      dat<-data.frame(tibble(Name = colnames(initial), Class = sapply(initial, function(x) 
        paste(class(x), collapse=", "))))
      
      datatable(dat)
    }) 
    
  }
)

关键修改说明

  • UI端:将datatable("dt")替换为dataTableOutput("dt"),这是Shiny中输出DT表格的标准组件,用于接收server端output$dt生成的内容。
  • Server端代码无需修改,renderDataTable中返回datatable(dat)的写法是正确的,因为dat是合法的二维数据框。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 14:20:40