1

我在这里找到了如何使用reachable函数制作表格的示例,但是没有找到(也在不同的来源中)如何做双列名称

library(reachable)
library(data.table) 

data<-structure(list(date = structure(c(1580428800, 1582848000, 1585612800, 
1588204800, 1580428800, 1582848000, 1585612800, 1588204800, 1580428800, 
1582848000, 1585612800, 1588204800, 1580428800, 1582848000, 1585612800, 
1588204800, 1580428800, 1582848000, 1585612800, 1588204800, 1580428800, 
1582848000, 1585612800, 1588204800, 1580428800, 1582848000, 1585612800, 
1588204800, 1580428800, 1582848000, 1585612800, 1588204800, 1588204800
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), group = c("a", 
"a", "a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b", 
"b", "b", "c", "c", "c", "c", "c", "c", "c", "c", "d", "d", "d", 
"d", "d", "d", "d", "d", "e"), value = c(54.3923333333333, 52.278, 
43.5828333333333, 41.39875, 54.9545, 53.12025, 46.044, 44.4135, 
-14.8910166666667, -12.3993916666667, -9.64581666666667, -9.449925, 
-13.4157916666667, -11.1878416666667, -9.567625, -9.47255833333334, 
92, 66, 42, 63.75, 81, 55, 31, 59.375, 916.175525, 739.877458333333, 
533.883366666667, 658.222004166667, 955.311075, 752.5635875, 
531.2294, 722.806975, 722.806975), multiplication_factor = c(1, 
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 33, 34, 35, 36, 37, 
38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51)), row.names = c(NA, 
-33L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000012c2e1a1ef0>)


data<-dcast(setDT(data), group ~ date, value.var=c("value", "multiplication_factor"))
reactable(data)

但是,使用此代码reachable() 创建了一个表,date并且value/multiplication_factor被连接在一起以显示列名。我想让列名的第一行/层显示date,第二行显示变量(value/ multiplication_factor

是否可以使用相同的功能做与此表类似的事情?

在此处输入图像描述

4

1 回答 1

2

这可以通过列组来实现。按照reactable 文档中的示例并使用lapply

library(reactable)
library(data.table)

data_wide <- dcast(setDT(data), group ~ date, value.var = c("value", "multiplication_factor"))

cols <- names(data_wide)[!names(data_wide) %in% c("group")]
cols <- setNames(cols, cols)

col_groups <- as.character(unique(data$date))

columns <- lapply(cols, function(x) colDef(name = if (grepl("^value", x)) "value" else "multiplication_factor"))
columnGroups <- lapply(col_groups, function(x) {
  cols <- unlist(cols[grepl(x, names(cols))])
  colGroup(name = x, columns = unname(cols))
})

reactable(
  data_wide,
  columns = columns,
  columnGroups = columnGroups
)

于 2021-07-27T14:28:39.110 回答