I'm making automated reports using R Markdown and my users want the group header in their tables to be centered and to appear larger than the rest of the text. Specifically, in the following table output, I want to make Species larger and centered.
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")
I tried inserting formatting options within pack_rows(), but that's not how pack_rows() works so far as I can tell. As this is for rows, not columns, the options which work for add_header_above() do not work here. Plus, it's not a hardcoded header name, nor can it be.
Thanks in advance!!
Here are a couple of possible solutions, one each for html and pdf.
html version first as this seems to be the approach you are using:
---
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 output (exaggerated font size for effect):
This requires pre-formatting the group headings as LaTeX with a selected font size. I've just used typical LaTeX approach with relative font sizes. It would be possible to add exact sizes with additional LaTeX packages.
---
output: pdf_document
---
```{r pdf-answer, warning=FALSE, message=FALSE}
library(kableExtra)
library(forcats)
library(dplyr)
iris1 <-
iris |>
group_by(Species) |>
slice_head(n = 5) |>
mutate(Species = paste0(paste("\\\\begin{LARGE}", Species, "\\\\end{LARGE}")))
kable(iris1[,-5],
format = "latex",
booktabs = TRUE)|>
row_spec(which(iris1$Sepal.Length < 5),
bold = TRUE,
background = "red") |>
pack_rows(index = table(fct_inorder(iris1$Species)),
latex_align = "c",
escape = FALSE)
```
pdf output: