是否可以在可反应库中创建双列名称?
Is it possible to create double column names in reactable library?
我找到了 here 个例子,如何用 reachable
函数做 tables,但是,没有找到(也在不同的来源)如何做双列名称
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() 创建一个 table,但是,date
和 value
/multiplication_factor
连接在一起以显示列名。
我想让列名的第一个 row/layer 显示 date
,第二个显示变量 (value
/multiplication_factor
)
是否可以使用相同的函数做与此类似的事情table?
这可以通过列组来实现。按照 reactable
docs 中的示例并使用 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
)
我找到了 here 个例子,如何用 reachable
函数做 tables,但是,没有找到(也在不同的来源)如何做双列名称
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() 创建一个 table,但是,date
和 value
/multiplication_factor
连接在一起以显示列名。
我想让列名的第一个 row/layer 显示 date
,第二个显示变量 (value
/multiplication_factor
)
是否可以使用相同的函数做与此类似的事情table?
这可以通过列组来实现。按照 reactable
docs 中的示例并使用 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
)