【问题标题】:How to extend a table cell across multiple columns when rendered in R Shiny?在 R Shiny 中呈现时,如何跨多个列扩展表格单元格?
【发布时间】:2018-09-23 01:04:40
【问题描述】:

我有一个值数据框,我试图在 R Shiny 中将其呈现为表格。我想扩展某些值以占用多个列,就像您使用 HTML rowspan 属性一样。但是,如果我要这样做,我必须从头开始创建整个表,而且我更喜欢使用 DT 库来轻松地从我的数据表进行转换。

例如,我创建了以下脚本:

# ui.R

fluidPage(
  DT::dataTableOutput("table")
)

# server.R

library(DT)

function(input, output) {

  output$table <- DT::renderDataTable({
    df <- data.frame(A = c("Trial 1", 1, 1, 1), B = c("", 1, 1, 1), C = c("", 1, 1, 1),
                     D = c("Trial 2", 2, 2, 2), E = c("", 2, 2, 2), F = c("", 2, 2, 2),
                     G = c("Trial 3", 3, 3, 3), H = c("", 3, 3, 3), I = c("", 3, 3, 3))

    DT::datatable(df,
                  options = list(dom = "t",
                                 ordering = FALSE))
  })

}

此脚本呈现以下 UI:

如何展开包含“试用 1”、“试用 2”和“试用 3”的单元格,使它们各占三列?

【问题讨论】:

  • 将第一行作为分组标题添加到普通列名上方是否合适?
  • 理想情况下,我希望能够定位任何单元格,而不仅仅是标题单元格。

标签: r html-table shiny


【解决方案1】:

我们可以使用自定义容器将列组名称添加为真正的列标题:

# ui.R

ui <- fluidPage(
  DT::dataTableOutput("table")
)

# server.R

library(DT)
df <- data.frame(A = c(1, 1, 1), B = c(1, 1, 1), C = c(1, 1, 1),
                 D = c(2, 2, 2), E = c(2, 2, 2), F = c(2, 2, 2),
                 G = c(3, 3, 3), H = c(3, 3, 3), I = c(3, 3, 3))

myContainer <- htmltools::withTags(table(
  class = 'display',
  thead(
    tr(
      th(),
      th(colspan = 3, 'Trial 1', class = "dt-center"),
      th(colspan = 3, 'Trial 2', class = "dt-center"),
      th(colspan = 3, 'Trial 3', class = "dt-center")
    ),
    tr(
      th(),
      lapply(names(df), th)
    )
  )
))

server <- function(input, output) {

  output$table <- DT::renderDataTable({
    DT::datatable(df, container = myContainer,
                  options = list(dom = "t", ordering = FALSE,
                                 columnDefs = list(list(className = "dt-center", targets = "_all"))
                  ))
  })

}

runApp(list(ui = ui, server = server))

输出:

另外,如果您真的希望第一行有 colspans,我们可以使用 initComplete 选项在呈现表格时调用 JavaScript 函数。这里只是服务器部分:

jsc <- '
function(settings, json) {
  $("td:contains(\'Trial\')").attr("colspan", "3").css("text-align", "center");
  $("tbody > tr:first-child > td:empty").remove();
}'

server <- function(input, output) {
  output$table <- DT::renderDataTable({
    df <- data.frame(A = c("Trial 1", 1, 1, 1), B = c("", 1, 1, 1), C = c("", 1, 1, 1),
                     D = c("Trial 2", 2, 2, 2), E = c("", 2, 2, 2), F = c("", 2, 2, 2),
                     G = c("Trial 3", 3, 3, 3), H = c("", 3, 3, 3), I = c("", 3, 3, 3))
    DT::datatable(df, options = list(dom = "t", ordering = FALSE, initComplete = JS(jsc)))
  })

}

在JS函数的第一行,我们选择所有包含Trial字样的单元格,并添加相应的属性和样式。之后,我们选择所有空单元格,它们是行元素的直接后代,而行元素又是表格主体的第一个子元素,并将它们从 DOM 中删除。 Here 您可以找到有关 CSS 选择器的一般参考,例如 &gt;。

输出:

【讨论】:

    猜你喜欢
    • 2013-10-24
    • 1970-01-01
    • 2021-10-27
    • 2013-11-02
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    相关资源
    最近更新 更多