[英]Grouping Columns with DT Package in R
您可以使用自定義表格容器(參見此處: https : //rstudio.github.io/DT/ )和一些 JS (jQuery) 來修改表格 CSS 樣式。
# a custom table container
sketch = htmltools::withTags(table(
class = 'display',
thead(
# Define the grouping of your df
tr(
th(colspan = 4, 'Historical'),
th(colspan = 4, 'Current')
),
# Repeat column names 8 times
tr(
lapply(paste0("Col ", 1:8), th)
)
)
))
# Using JS for adding CSS, i.e., coloring your heading
# Get the corresponding table header (th) from a table cell (td) and apply color to it
headjs <- "function(thead) {
$(thead).closest('thead').find('th').eq(0).css('background-color', '#D9E1F2');
$(thead).closest('thead').find('th').eq(1).css('background-color', '#8EA9DB');
}"
# Your data frame
df <- data.frame(1,2,3,4,5,6,7,8)
# Output DT with your custom header
datatable(df , container = sketch, options = list(
headerCallback = JS(headjs)
))
和輸出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.