gideon1321
gideon1321

Reputation: 61

Export flextable to word in R

i am new to R. I am very grateful for every help. I created a flextable and want to export the files to a word document. This works fine. I used a tempfile as suggested "https://davidgohel.github.io/flextable/reference/save_as_docx.html". I tried to export or save this tempfile to my working directory, but this is not working:

 library(officer)
ft1 <- as_flex_table (ex_tbl)
sect_properties <- prop_section(
  page_size = page_size(orient = "landscape",
                        width = 8.3, height = 11.7),
  type = "continuous")
  
save_as_docx(ft1, path = tf)
save_as_docx(`Table 1` = ft1, path = **("\\Users\\XXX\\Desktop\\example_2.docx")**, pr_section = sect_properties).
 save_as_docx(`Table 1` = ft1, path = ("\\Users\\XXX\\Desktop\\example_2.docx"), pr_section = sect_properties)

=> Error: directory of \Users\XXX\Desktop\example_2.docx does not exist. Furthermore the print function did not work.

print(tf, target = "c:/Users/Hendrik/Desktop/")

Is it possible to save the tempfile directly in the working directory

Upvotes: 1

Views: 7544

Answers (1)

hamza
hamza

Reputation: 31

Try to use the following code:

flextable(your_table) %>% save_as_docx( path = "name.docx")
# The output will be saved to the work space.

Example:

df <- read.table(header=TRUE, text='
 id age
  1     20
  2     27
  3     24
  4     26
  5     20
')

stats <- df  %>% summarise(N = n(),mean = mean(age),
         std=round(sd(age),2),max = max(age),min = min(age))

flextable(stats) %>% save_as_docx( path = "stats.docx")

Upvotes: 3

Related Questions