【问题标题】:`gtsummary` and `papaja` integration for `kable` tables`kable`表的`gtsummary`和`papaja`集成
【发布时间】:2021-10-02 07:59:39
【问题描述】:

我正在尝试在 papaja::apa6_pdf 文档中使用 gtsummary 表来包含格式化(带标题)kable 表。但是,它没有按预期呈现。相比之下,gtsummarykable 表在普通的rmarkdown::pdf_document 中渲染得很好(尽管gtsummarykableExtra 表看起来也不太好)。对于如何让gtsummarypapaja 一起发挥作用以生成“漂亮”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()
```

papaja::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()
```

【问题讨论】:

  • 我不确定为什么 kable 输出无法识别降价 ** 以加粗 papaja 输出中的标题。但是您可以用modify_header(list(all_stat_cols() ~ "{level}, N = {n}", label ~ "Characteristic")) 替换标题以不包含粗体

标签: r r-markdown kable gtsummary papaja


【解决方案1】:

可能最通用的解决方案是在as_kable()中指定表格输出格式。

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.:也可以全局指定表格输出格式。在 papaja 文档中,您可以将以下行添加到您的 setup 块中。

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

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

【讨论】:

  • 谢谢!这更好,但仍然没有给我我需要的东西。例如,粗体行标签在 pandoc ` trial %>% tbl_summary(by = trt) %>% bold_labels() %>% caption = "A table", format.args = list(na_string = "") 中没有正确解释, 格式 = "pandoc" ) as_tibble() %>% apa_table(...) `
  • 嗨@maia-sh,我刚刚用更通用的解决方案更新了我的答案。
  • as_kable(format = 'pipe') 似乎是一个神奇的解决方案!谢谢
猜你喜欢
  • 2020-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 1970-01-01
  • 2017-11-13
  • 1970-01-01
相关资源
最近更新 更多