Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting several tables to word documents using r package 'flextable'

I am trying to export two (flex)tables within the same word document. Here what I have:

df1 <- data.frame(a = 1:3, b = 1:3)
df2 <- data.frame(c = 11:13, d = 11:13)

library(flextable)
df1_ft <- regulartable(df1)
df2_ft <- regulartable(df2)

library(officer)
word_export <- read_docx()
word_export <- body_add_flextable(word_export, df1_ft)
word_export <- body_add_flextable(word_export, df2_ft)
print(word_export, 'try.docx')

Yet, the results is a single table containing both tables. Any idea how I can add to two tables one after the other?

like image 952
Rtist Avatar asked Oct 15 '25 15:10

Rtist


1 Answers

The two tables are being output to Word one after the other, and as there are no line breaks between them they appear as one. Simplest solution is to place a paragraph with empty text between them:

word_export <- read_docx()
body_add_flextable(word_export, df1_ft)
body_add_par(word_export, value = "")
body_add_flextable(word_export, df2_ft)
print(word_export, 'try.docx')

enter image description here

You can also see that you don't need to repeatedly assign the output of each body_add_* line back to your word_export file - the Word file is updated directly by each call.

like image 164
Stewart Ross Avatar answered Oct 18 '25 07:10

Stewart Ross



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!