0

I am trying to use a gtsummary table within a papaja::apa6_pdf document to include a formatted (with caption) kable table. However, it's not rendering as expected. In contrast, the gtsummary kable table renders well in a normal rmarkdown::pdf_document (though gtsummary kableExtra table also doesn't look great). I'd appreciate any suggestions on how to get gtsummary and papaja to play well together to produce a "pretty" PDF table. Thank you!

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

gtsummary-pdf_output

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

gtsummary-papaja

maia-sh
  • 537
  • 4
  • 14
  • I am not sure why the kable output is not recognizing the markdown `**` to bold the header in the papaja output. But you can replace the header to not include the bolding with `modify_header(list(all_stat_cols() ~ "{level}, N = {n}", label ~ "Characteristic"))` – Daniel D. Sjoberg Jul 26 '21 at 12:41

1 Answers1

2

Probably the most general solution is to specify the table output format in as_kable().

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

The PDF then looks like the following: enter image description here

This also seems to work with bold labels:

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

The PDF then looks like the following: enter image description here

P.S.: It is also possible to specify the table output format globally. In a papaja document, you could add the following line to your setup chunk.

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

If added, you can then completely omit the call to as_kable() (but a warning message will be printed).

Marius Barth
  • 596
  • 2
  • 9
  • Thank you! This is better but still not giving me what I need. For example, bold row labels are not properly interpreted in pandoc ` trial %>% tbl_summary(by = trt) %>% bold_labels() %>% caption = "A table", format.args = list(na_string = ""), format = "pandoc" ) as_tibble() %>% apa_table(...) ` – maia-sh Jul 30 '21 at 14:43
  • Hi @maia-sh, I just updated my answer with a more general solution. – Marius Barth Aug 06 '21 at 12:21
  • `as_kable(format = 'pipe')` was the magic solution it seems! Thank you – maia-sh Aug 19 '21 at 08:33