Search code examples
rtidyverseflextableofficerofficedown

insert space after each flextable in r (Rmd)


i have this problem:

Problem:

I have multiple flextable objects in a same r chunk:

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

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

But 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. So i manage to solve it this way:

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. So what i want to do know is to join this two functions into one so that i don't have to use two different functions. Something like this:

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.

Objetive:

  • Insert a line (an Enter) after each flextable object with a single line of code.

Attempts:

  • Apparently a function in r can't retrieve an object and a function even if you use return() multiple times.

Thanks.


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