1

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

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
gideon1321
  • 51
  • 1
  • 5
  • You don't need a tempfile. The tempfile in the documentation is just meant for example purposes. The issue with using the print is probably that you passed tempfile `tf` as the first argument instead of the flexible object `ft1`. – stefan Nov 28 '21 at 00:01
  • Thank you very much Stefan. I really appreciate your very quick support – gideon1321 Nov 28 '21 at 07:42

1 Answers1

2

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")
Martin Gal
  • 16,640
  • 5
  • 21
  • 39
hamza
  • 21
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Fahmida Apr 04 '22 at 16:33