rr-markdownflextable

How to add new line between tables made from flextable in an RMarkdown code chunk?


I am trying to add a new line between flextables in a code chunk. Line and page breaks interact with the table caption of the second line causing it to be displayed. How can I add a line break between the tables in a code chunk without causing the problem with caption.

The code I have been using is below. The commented cat and knitr statements are attempts I've made to add a new line between the tables. Those solutions came from a few questions: https://stackoverflow.com/questions/59824514/new-line-after-table-generated-with-kable https://stackoverflow.com/questions/49561077/creating-a-new-line-within-an-rmarkdown-chunk https://stackoverflow.com/questions/65240436/how-to-add-a-line-break-in-rmarkdown-code

---
title: "Table Loop Example"
output: bookdown::word_document2
---

```{r echo = FALSE, message=FALSE, results='asis'}
library(officer)
library(flextable)
library(tidyverse)

df_1 <- data.frame(replicate(4,sample(0:10,5,rep=TRUE)))
df_2 <- data.frame(replicate(5,sample(5:12,6,rep=TRUE)))

ft <- flextable(df_1) %>% set_caption(caption = "Dataframe 1", autonum = run_autonum(seq_id = "tab", bkm = "tab1")) %>% paginate(init=TRUE, hdr_ftr = TRUE)
flextable_to_rmd(ft)

#cat("  \n")
#cat("\n ")
#cat("<br>")
#cat("<br/>")
#cat("\\")
#cat("\n \\newline \n")
#knitr::asis_output("\n \\newline \n")

ft <- flextable(df_2) %>% set_caption(caption = "Dataframe 2", autonum = run_autonum(seq_id = "tab", bkm = "tab2")) %>% paginate(init=TRUE, hdr_ftr = TRUE)
flextable_to_rmd(ft)
```

The cat(" \n") command produces no change in the document. The other commands interact with the caption of the second table.

Table Loop Example
Table 1: Dataframe 1
X1  X2  X3  X4
9   5   7   10
1   3   6   10
9   0   8   2
1   4   5   0
6   7   4   5
 ::: {custom-style=“Table Caption”}
Table 2: Dataframe 2
:::
X1  X2  X3  X4  X5
5   5   11  7   9
12  11  12  9   8
6   6   8   7   7
6   6   7   12  8
12  9   8   9   8
9   7   7   8   6

I want it to look like this:

Table Loop Example
Table 1: Dataframe 1
X1  X2  X3  X4
9   5   7   10
1   3   6   10
9   0   8   2
1   4   5   0
6   7   4   5

Table 2: Dataframe 2
X1  X2  X3  X4  X5
5   5   11  7   9
12  11  12  9   8
6   6   8   7   7
6   6   7   12  8
12  9   8   9   8
9   7   7   8   6

Solution

  • Here is a way. I don't really like it because it uses some unexported package functions. But it works.

    ```{r echo = FALSE, message=FALSE, results='asis'}
    library(officer)
    library(flextable)
    library(magrittr)
    
    df_1 <- data.frame(replicate(4,sample(0:10,5,rep=TRUE)))
    df_2 <- data.frame(replicate(5,sample(5:12,6,rep=TRUE)))
    
    ft1 <- flextable(df_1) %>% 
      set_caption(
        caption = "Dataframe 1", 
        autonum = run_autonum(seq_id = "tab", bkm = "tab1")
      ) %>% paginate(init=TRUE, hdr_ftr = TRUE)
    flextable_to_rmd(ft1)
    
    # line break
    x <- officer:::to_wml(fpar(run_linebreak()))
    cat(flextable:::with_openxml_quotes(x))
    
    ft2 <- flextable(df_2) %>% 
      set_caption(
        caption = "Dataframe 2", 
        autonum = run_autonum(seq_id = "tab", bkm = "tab2")
      ) %>% paginate(init=TRUE, hdr_ftr = TRUE)
    flextable_to_rmd(ft2)
    ```