使用带有索引的 pack_rows() 时如何调整 kableExtra 表分组标题的字体大小和对齐方式?

问题描述 投票:0回答:1

我正在使用 R Markdown 制作自动报告,我的用户希望表格中的组标题居中并显示得比文本的其余部分大。具体来说,在下表输出中,我想让 Species 更大并居中。

library(kableExtra)

kable(iris,escape = F)%>%
  pack_rows(index = table(fct_inorder(iris$Species)))%>%
  kable_styling(bootstrap_options = "striped", full_width = T, position = 'center')%>%
  row_spec(which(iris$Sepal.Length < 5), bold = T, background = "red")

我尝试在 pack_rows() 中插入格式化选项,但据我所知,这不是 pack_rows() 的工作原理。由于这是针对行而不是列,因此适用于 add_header_above() 的选项在这里不起作用。另外,它不是硬编码的标头名称,也不可能是。

提前致谢!

r r-markdown kableextra
1个回答
0
投票

这里有几种可能的解决方案,html 和 pdf 各一种。

首先是 html 版本,因为这似乎是您正在使用的方法:

--- output: html_document --- ```{r packages, warning=FALSE, message=FALSE} library(kableExtra) library(forcats) library(dplyr) ``` ```{r html-answer} iris1 <- iris |> group_by(Species) |> slice_head(n = 5) kable(iris1[,-5], format = "html")|> kable_styling(bootstrap_options = "striped", full_width = TRUE, position = 'center') |> row_spec(which(iris1$Sepal.Length < 5), bold = TRUE, background = "red") |> pack_rows(index = table(fct_inorder(iris1$Species)), label_row_css = "font-size: 30px; text-align: center;") ```
html输出(夸大字体大小以达到效果):

© www.soinside.com 2019 - 2024. All rights reserved.