[英]R: Aligning nested headers in DT table
我想生成一個帶有三重嵌套標題的DT
表,但我沒有正確對齊。 雙嵌套的事情非常簡單,請參見下面的代碼:
library(DT)
library(dplyr)
### yearly breakdown
df_year <- data.frame(
group = LETTERS[1:6],
year = rep(2017, 6),
A_2017 = rnorm(6),
B_2017 = rnorm(6)
)
sketch_year = htmltools::withTags(table(class = 'display',
thead(tr(
th(rowspan = 2, 'Group'),
lapply(unique(df_year$year),
th, colspan = 2)
),
tr(
lapply(rep(c(
'Alpha', 'Beta'
), length(unique(df_year$year))), th)
))))
DT::datatable(select(df_year, -year),
container = sketch_year,
rownames = FALSE,
fillContainer = TRUE)
但是,如果我嘗試將類似的原則應用於第三行,則它似乎未對齊:
## quarterly breakdown
df_qrt <- data.frame(
group = LETTERS[1:6],
year = rep(2017, 6),
Q1_2017A = rnorm(6),
Q1_2017B = rnorm(6),
Q2_2017A = rnorm(6),
Q2_2017B = rnorm(6),
Q3_2017A = rnorm(6),
Q3_2017B = rnorm(6),
Q4_2017A = rnorm(6),
Q4_2017B = rnorm(6)
)
sketch_qrt = htmltools::withTags(
table(class = 'display',
thead(tr(
th(rowspan = 2, 'Group'),
lapply(unique(df_qrt$year),
th, colspan = 8)
),
tr(
lapply(paste0('Q', 1:4),
th, colspan = 2)
),
tr(lapply(rep(
c('Alpha', 'Beta'), 4
), th))
))
)
DT::datatable(select(df_qrt, -year),
container = sketch_qrt,
rownames = FALSE,
fillContainer = TRUE)
我怎樣才能讓它正確對齊? 謝謝。
在這種情況下,對齊被拋出,因為“組”標題沒有更改為跨越新創建的行。
sketch <- htmltools::withTags(
table(class = 'display',
thead(tr(
th(rowspan = 3, 'Group'),
lapply(unique(df$year),
th, colspan = 8)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.