R reactable格式多列为百分比

cgvd09ve  于 2023-07-31  发布在  React
关注(0)|答案(1)|浏览(78)

我想在我的dataframe格式与reactable包的百分比几列。我知道如果我一行一行地指定所有这些,我该怎么做,但我想只有在指定起始列号时才能实现相同的输出,在本例中是从第2列开始。

reactable(
  data.frame(
    id = seq(1, 10, 1),
    sh = seq(.1, 1, .1),
    ar = seq(.1, 1, .1),
    br = seq(.1, 1, .1)
  ), 
  columns = list(
    sh = colDef(format = colFormat(percent = TRUE, digits = 1)),
    ar = colDef(format = colFormat(percent = TRUE, digits = 1)),
    br = colDef(format = colFormat(percent = TRUE, digits = 1))
    )
  )

字符串

sczxawaw

sczxawaw1#

一种选择是使用例如lapply创建colDef的命名列表:

library(reactable)

dat <- data.frame(
  id = seq(1, 10, 1),
  sh = seq(.1, 1, .1),
  ar = seq(.1, 1, .1),
  br = seq(.1, 1, .1)
)

cols <- names(dat)[-1]
col_defs <- lapply(
  cols,
  \(x) colDef(format = colFormat(percent = TRUE, digits = 1))
)
names(col_defs) <- cols

reactable(
  dat,
  columns = col_defs
)

字符串


的数据

相关问题