【问题标题】:Is it possible to create double column names in reactable library?是否可以在可反应库中创建双列名称?
【发布时间】:2021-10-03 07:38:53
【问题描述】:

我找到了here 示例,关于如何使用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() 创建一个表,但是,datevalue/multiplication_factor 连接在一起以显示列名。 我想让列名的第一行/层显示date,第二行显示变量(value/multiplication_factor

是否可以使用相同的函数来做类似这张表的事情?

【问题讨论】:

    标签: r reactable


    【解决方案1】:

    这可以通过列组来实现。按照reactabledocs中的例子,使用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
    )
    

    【讨论】:

    • 感谢您的帮助。您能否帮助我如何为group 列插入搜索过滤器?它应该类似于下面的内容,但我不知道该怎么做。 reactable( data_wide, columns = list( group = colDef(filterable = NULL), columns), columnGroups = columnGroups )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 2015-05-15
    相关资源
    最近更新 更多