Search code examples
rr-markdownkablegtsummarypapaja

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


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

gtsummary-papaja


Solution

  • 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).