Search code examples
ms-wordr-markdownflextable

Rmd table captions are messed up when knitting to word doc


I am trying to make tables in Rmd, with different captions or headers. The package flextable has great options that can be output to word documents. It's function add_header_lines() allows you to add a caption header at the top of each table. When Rmd output wraps to a new page, add_header_lines() adds another caption header to the top of the continued table on the next page. However, it grabs the arguments passed to whatever table you made first. Then it continues to be correct for the next table, until the next page is reached, where it reverts to the first again (see pictures).

Here is a reproducible example, where all values in a table should be the same as the Table number.

Any ideas on how to fix this? I would like it to have the correct caption, but would settle for simply getting rid of the second caption after a page break.

    ---
title: "Untitled"
author: "Anyone"
date: "2/29/2020"
output:
  word_document: default
  html_document:
    df_print: paged
  pdf_document: default
---

```{r setup, include=FALSE}
library(dplyr)
library(flextable)

knitr::opts_chunk$set(echo = FALSE,message=FALSE)

```


```{r Table1}
cars1<-cars*0+1
theme_zebra(regulartable(cars1))%>% 
  align(align="center",part="all") %>% 
  autofit%>% 
  add_header_lines(paste("Table 1: Model output for thing 1"))

```

```{r Table2}
cars2<-cars*0+2
theme_zebra(regulartable(cars2))%>% 
  align(align="center",part="all") %>% 
  autofit%>% 
  add_header_lines(paste("Table 2: Model output for thing 2"))

```

```{r Table3}
cars3<-cars*0+3
theme_zebra(regulartable(cars3))%>% 
  align(align="center",part="all") %>% 
  autofit%>% 
  add_header_lines(paste("Table 3: Model output for thing 3"))

```

```{r Table4}
cars4<-cars*0+4
theme_zebra(regulartable(cars4))%>% 
  align(align="center",part="all") %>% 
  autofit%>% 
  add_header_lines(paste("Table 4: Model output for thing 4"))

```

Word output page 1

Note that this is where the page break is. The table labelled '1' here, should really be '2.' It will continue to do this for every page, where the rest of the table is labelled with a '1' (try Rmd code).

Word output page 2


Solution

  • That's Word fault :) It decides that two tables with no break between them is the same table.

    ---
    title: "Untitled"
    author: "Anyone"
    date: "2/29/2020"
    output:
      word_document: default
    ---
    
    ```{r setup, include=FALSE}
    library(dplyr)
    library(flextable)
    
    knitr::opts_chunk$set(echo = FALSE,message=FALSE)
    
    ```
    
    
    ```{r Table1}
    cars1<-cars*0+1
    theme_zebra(flextable(cars1))%>% 
      align(align="center",part="all") %>% 
      autofit%>% 
      add_header_lines(paste("Table 1: Model output for thing 1"))
    
    ```
    
    blah
    
    ```{r Table2}
    cars2<-cars*0+2
    theme_zebra(flextable(cars2))%>% 
      align(align="center",part="all") %>% 
      autofit%>% 
      add_header_lines(paste("Table 2: Model output for thing 2"))
    
    ```
    
    blah
    
    ```{r Table3}
    cars3<-cars*0+3
    theme_zebra(flextable(cars3))%>% 
      align(align="center",part="all") %>% 
      autofit%>% 
      add_header_lines(paste("Table 3: Model output for thing 3"))
    
    ```
    
    blah
    
    ```{r Table4}
    cars4<-cars*0+4
    theme_zebra(flextable(cars4))%>% 
      align(align="center",part="all") %>% 
      autofit%>% 
      add_header_lines(paste("Table 4: Model output for thing 4"))
    
    ```
    

    enter image description here

    As far as i know, this is not something that can be solved with flextable but if there is an option I am not aware of, I'd be happy to integrate it.