rofficerr-flextableofficedown

insert space after each flextable in r (Rmd)


I have multiple flextable objects in the same R chunk:

mtcars %>% 
  select(1:3) %>% 
  head() %>% 
  flextable()

mtcars %>% 
  select(1:3) %>% 
  head() %>% 
  flextable()

When I knit the Rmd in a officedown::rdocx_document the tables appear stick together like this: tables stick

This is a problem because having multiple tables stick changes the sizes of the following tables and its taken by Word as a single big table.

I managed to solve it this way:

```{r, results='asis'}
library(tidyverse)
library(flextable)
library(officer)
library(officedown)

mtcars %>% 
  select(1:3) %>% 
  head() %>% 
  flextable()

officer::run_linebreak()

mtcars %>% 
  select(1:3) %>% 
  head() %>% 
  flextable()

Now tables have a line or Enter in between. What I want to do now is to join this two functions into one so that I don't have to use two different functions. Something like this:

```{r, results='asis'}
mtcars %>% 
  select(1:3) %>% 
  head() %>% 
  table_and_enter()

mtcars %>% 
  select(1:3) %>% 
  head() %>% 
  table_and_enter()

What I actually want is a space between tables, so if you find a more efficient way to do it feel free to suggest that.

Objective:

Attempts:


Solution

  • Adding a caption fixes it. Here, I added blank captions:

    ft <- mtcars %>% 
      select(1:3) %>% 
      head() %>% 
      flextable()
    ft <- set_caption(ft, "")
    ft
    
    
    ft <- mtcars %>% 
      select(1:3) %>% 
      head() %>% 
      flextable()
    ft <- set_caption(ft, "")
    ft
    

    I ran this in a R markdown chunk with echo = FALSE and used the word_ouput option. The result is nicely separated.

    enter image description here