2

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.

  • 1
    Providing text instead of images helps to get faster recommendations from the community – RF1991 Mar 29 '22 at 20:47
  • 1
    I get two separate tables when I run the code in the top code block, and there are no vertical lines in the output. Also the `return` function is not a carriage return. It is designed to send values from inside a function to the calling environment. – IRTFM Mar 30 '22 at 01:03

2 Answers2

1

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

markhogue
  • 1,056
  • 1
  • 6
  • 16
0

If you want return() to return multiple objects from a function you can put the objects in a list and return the list.


my_fun <- function(x, y){
  x = x+1
  y = x*y
  
  the_list <- list(x, y)
  
  return(the_list)
}

my_fun(3, 4)
kpress
  • 136
  • 6
  • thanks kpress for the response. Its not quite im looking for because when i knitr my officedown document with the solution you suggest it appears like this: [[1]] [[2]] list() attr(,"class") [1] "run_linebreak" "run" – Santiago Sotelo Apr 07 '22 at 05:22