问题描述
我正在尝试在 gtsummary
文档中使用 papaja::apa6_pdf
表来包含格式化(带标题)kable
表。但是,它没有按预期呈现。相比之下,gtsummary
kable
表在普通 rmarkdown::pdf_document
中呈现得很好(尽管 gtsummary
kableExtra
表看起来也不太好)。对于如何让 gtsummary
和 papaja
很好地协同工作以生成“漂亮”的 PDF 表格的任何建议,我将不胜感激。谢谢!
rmarkdown::pdf_document
```
---
title: "gtsummary + rmarkdown::pdf_document"
output: pdf_document
---
```
```{r}
library(gtsummary)
trial %>%
tbl_summary(by = trt) %>%
modify_caption("This is a table about trials") %>%
as_kable()
trial %>%
tbl_summary(by = trt) %>%
modify_caption("This is another table about trials") %>%
as_kable_extra()
```
木瓜::apa6_pdf
```
---
title : "gtsummary + papaja"
shorttitle : "gtsummary + papaja"
author:
- name : "First Author"
affiliation : "1"
corresponding : yes # Define only one corresponding author
address : "Postal address"
email : "[email protected]"
affiliation:
- id : "1"
institution : "Wilhelm-Wundt-University"
authornote: >
abstract: "my abstract"
keywords : "keywords"
wordcount : "X"
bibliography : []
floatsintext : no
figurelist : no
tablelist : no
footnotelist : no
linenumbers : yes
mask : no
draft : no
documentclass : "apa6"
classoption : "man"
output : papaja::apa6_pdf
---
```{r}
library(papaja)
library(gtsummary)
trial %>%
tbl_summary(by = trt) %>%
modify_caption("This is a table about trials") %>%
as_kable()
trial %>%
tbl_summary(by = trt) %>%
modify_caption("This is another table about trials") %>%
as_kable_extra()
```
解决方法
可能最通用的解决方案是在 as_kable()
中指定表格输出格式。
trial %>%
tbl_summary(by = trt) %>%
modify_caption("This is a table about trials") %>%
as_kable(format = 'pipe')
这似乎也适用于粗体标签:
trial %>%
tbl_summary(by = trt) %>%
modify_caption("This is a table about trials") %>%
bold_labels() %>%
as_kable(format = 'pipe')
P.S.:也可以全局指定表格输出格式。在 papaja 文档中,您可以将以下行添加到您的 setup 块中。
options(knitr.table.format = 'pipe')
如果添加,则您可以完全省略对 as_kable()
的调用(但会打印一条警告消息)。