Search code examples
rr-markdownflextablegtsummaryofficedown

Table captions underneath tables if using gtsummary + flextable in officedown


I feel like I am missing something easy. Fingers crossed thats it.

Im trying to produce tables using gtsummary and flextable in officedown. I'm using the yaml options from the "Advanced word document" template in officedown.

Below is my code. The first 2 tables have the captions underneath and the third is on top, as it should be!

R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
 [1] flextable_0.6.10     forcats_0.5.1        stringr_1.4.0       
 [4] dplyr_1.0.7          purrr_0.3.4          readr_2.0.2         
 [7] tidyr_1.1.4          tibble_3.1.5         ggplot2_3.3.5       
[10] tidyverse_1.3.1      gtsummary_1.5.0      palmerpenguins_0.1.0
[13] janitor_2.1.0        officer_0.4.1        officedown_0.2.3    


---
date:  "Last compiled on `r format(Sys.time(), '%d %B, %Y')`"
author: "Kristy Robledo"
title: "Table captions example"
output: 
  officedown::rdocx_document:
    mapstyles:
      Normal: ['First Paragraph']
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, fig.cap = TRUE)
library(officedown)
library(officer)
library(palmerpenguins)
library(gtsummary)
library(tidyverse)
library(flextable)
```


## My tables

```{r,tab.id="peng" }

penguins %>%
  tbl_summary(by=species) %>%
  as_flex_table() %>%
  set_caption("Flextable Caption by set_caption") 

```

```{r,tab.id="peng2", tab.cap="Caption by knitr" }

penguins %>%
  tbl_summary(by=species) %>%
  as_flex_table() 

```

### Tables from example

```{r tab.cap="caption 1", tab.id="mtcars"}
head(mtcars)
```


## Reference

see  table \@ref(tab:mtcars) for how I want it and these tables for underneath! 
(table \@ref(tab:peng) and  table \@ref(tab:peng2)!)

Solution

  • Not an expert in flextable and officedown. But one option which worked for me was to set the position of the table captions for the R Markdown document via

    knitr::opts_chunk$set(tab.topcaption = TRUE).

    Full reproducible code:

    ---
    date:  "Last compiled on `r format(Sys.time(), '%d %B, %Y')`"
    author: "Kristy Robledo"
    title: "Table captions example"
    output: 
      officedown::rdocx_document:
        mapstyles:
          Normal: ['First Paragraph']
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE, fig.cap = TRUE)
    library(officedown)
    library(officer)
    library(palmerpenguins)
    library(gtsummary)
    library(tidyverse)
    library(flextable)
    ```
    
    ```{r}
    knitr::opts_chunk$set(tab.topcaption = TRUE)
    ```
    
    ## My tables
    
    ```{r,tab.id="peng" }
    penguins %>%
      tbl_summary(by=species) %>%
      as_flex_table() %>%
      set_caption("Flextable Caption by set_caption") 
    
    ```
    
    ```{r, tab.id="peng2", tab.cap="Caption by knitr"}
    penguins %>%
      tbl_summary(by=species) %>%
      as_flex_table() 
    ```
    
    ### Tables from example
    
    ```{r tab.cap="caption 1", tab.id="mtcars"}
    head(mtcars)
    ```
    
    ## Reference
    
    see  table \@ref(tab:mtcars) for how I want it and these tables for underneath! 
    (table \@ref(tab:peng) and  table \@ref(tab:peng2)!)
    

    enter image description here