首页 > 解决方案 > 两列或多列上 DT::table 中的闪亮合并单元格

问题描述

类似于这个问题:Shiny: Merge cells in DT::datatable

我想知道是否有办法合并两列或更多列。在下面的示例中,仅合并了第 1 列中的重复行,如果我还想合并每个部分中第 5 列中的重复行怎么办。例如,在表格的图像中,我希望合并 Petal.Width 中的 0.2,以及 Petal.Width 中的 1.5。那可能吗?

library(shiny)
library(DT)

dat <- iris[c(1,2,3,51,52,53,101,102,103), c(5,1,2,3,4)]

ui <- fluidPage(
  DTOutput("table")
)

server <- function(input, output){
  output[["table"]] <- renderDT({
    dtable <- datatable(dat, rownames = FALSE, 
                        options = list(
                          rowsGroup = list(0) # merge cells of column 1
                        ))
    path <- "U:/Data/shiny/DT/www" # folder containing dataTables.rowsGroup.js
    dep <- htmltools::htmlDependency(
      "RowsGroup", "2.0.0", 
      path, script = "dataTables.rowsGroup.js")
    dtable$dependencies <- c(dtable$dependencies, list(dep))
    dtable
  })
}

shinyApp(ui, server)

在此处输入图像描述

标签: javascriptshinydt

解决方案


您需要做的就是使用您链接的答案(使用 datatables-rowsgroup 插件)并指定额外的列以合并重复的结果:

dtable <- datatable(
    dat,
    rownames = FALSE, 
    options = list(
       rowsGroup = list(0,4))) # merge cells of column 1 and 5

在此处输入图像描述


推荐阅读