问题描述:
我正在用 rMarkdown 编写文档,我需要在生成 PDF 之前格式化表格。问题是我在使用命令kable
添加的标题中插入新行时遇到问题。add_headers_above
目前,文本“Month (persons)” - 这是使用该命令添加的第一个标题 - 显示为一行。我想打破它,以便在“月”和“(人)”之间插入一个新行。
动机只是为了节省水平页面空间,以便我可以在一张 A4 纸上按大小放置两个表格。
我的修复尝试
我试图\n
在add_headers_above
命令中包含:
[...] %>%
add_header_above(c(" ", 'Month \n (persons)' = 1, "TTY \n (persons)" = 1, "TTY \n (% chg.)" = 1))
但这并没有奏效。
可重现的例子:
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(kableExtra)
library(tibble)
```
```{r}
df <- tribble(~Location, ~Jan_Qrt, ~Jan_year, ~growth,
"New South Wales", 16000, 403300, 3.30,
"Victoria", -21200, 134100, 3.50,
"Queensland", 2100, 100200, 3.20,
"South Australia", 19700, 117900, 5.00,
"Western Australia", 5300, 13200, 1.60,
"Tasmania", -8900, 24300, 1.90,
"Northern Territory", -400, 5700, 2.40,
"Australian Cap", 300, -4300, -3.10,
"Australia", 800, 10600, 4.80)
kable(df, format = 'latex', booktabs = T, align = 'r') %>%
kable_styling(full_width = F, font_size = 8) %>%
row_spec(row = 9, bold = TRUE) %>%
add_header_above(c(" ", 'Month (persons)' = 1, "TTY (persons)" = 1, "TTY (% chg.)" = 1))
```