`kable` 表的`gtsummary` 和`papaja` 集成

`gtsummary` and `papaja` integration for `kable` tables

我正在尝试在 papaja::apa6_pdf 文档中使用 gtsummary table 来包含格式化的(带标题)kable table。但是,它没有按预期呈现。相比之下,gtsummary kable table 在正常 rmarkdown::pdf_document 中呈现得很好(尽管 gtsummary kableExtra table 也没有看起来相当不错)。对于如何让 gtsummarypapaja 很好地协同工作以生成“漂亮”的 PDF table 的任何建议,我将不胜感激。谢谢!

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         : "my@email.com"

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() 中指定 table 输出格式。

trial %>%
  tbl_summary(by = trt) %>%
  modify_caption("This is a table about trials") %>%
  as_kable(format = 'pipe')

PDF 如下所示:

这似乎也适用于粗体标签:

trial %>%
  tbl_summary(by = trt) %>%
  modify_caption("This is a table about trials") %>%
  bold_labels() %>%
  as_kable(format = 'pipe')

PDF 如下所示:

P.S.: 也可以全局指定table输出格式。在 papaja 文档中,您可以将以下行添加到 setup 块中。

options(knitr.table.format = 'pipe')

如果添加,则可以完全省略对 as_kable() 的调用(但会打印一条警告消息)。