2

我将以下代码放在我的 R Markdown 文档中,它会根据我的数据框的结构很好地折叠行。如您所见,有一个align参数可以水平对齐数据。是否有类似的align_vertical论点可以让我垂直对齐?特别是我希望下面输出中的“a”和“b”垂直对齐。目前它们是垂直居中对齐的

library(kableExtra)
library(knitr)

collapse_rows_dt <- data.frame(C1 = c(rep("a", 10), rep("b", 5)),
                 C2 = c(rep("c", 7), rep("d", 3), rep("c", 2), rep("d", 3)),
                 C3 = 1:15,
                 C4 = sample(c(0,1), 15, replace = TRUE))

kable(collapse_rows_dt, "html", align = "c") %>%
  kable_styling(full_width = F) %>%
  column_spec(1, bold = T) %>%
  collapse_rows(columns = 1:2)
4

1 回答 1

2

如果您更改命令的顺序(column_spec最后执行) you can useextra_css to change thevertical-align` 属性:

library(kableExtra)
library(knitr)

collapse_rows_dt <- data.frame(C1 = c(rep("a", 10), rep("b", 5)),
                               C2 = c(rep("c", 7), rep("d", 3), rep("c", 2), rep("d", 3)),
                               C3 = 1:15,
                               C4 = sample(c(0,1), 15, replace = TRUE))

kable(collapse_rows_dt, "html", align = "c") %>%
  kable_styling(full_width = F) %>%
  collapse_rows(columns = 1:2) %>%
  column_spec(1, bold = T, extra_css = 'vertical-align: top !important;')

在此处输入图像描述

于 2018-03-05T12:23:15.120 回答