Creating Word Documents

I know this is an old debate, but I am looking for resources for producing word documents using R.

In my office, someone will hand me a word document full of wordy-stuff (page headers and footers, text boxes, custom section headings, two-column output, etc.) and ask me to insert unique plots and tables for a set of 64 to 250 unique locations. These plots and tables have size and formatting constraints. The person requesting the documents wants every document to appear identical AND they want to be able to edit the surrounding text after the individual documents have been produced. When I suggest something sensible like Rmarkdown, they claim that others want to be able to edit the text, people only have access to word, etc.

The response to my banging my head against my desk is usually a variation on the theme of "I designed this document in word. Why can't you do the same thing in R?"

So, I'd appreciate getting your thoughts about the best tools available for producing (occasionally) complicated word documents. I have looked at rmarkdown, officer, and wordR, but none of these tools have all of the capabilities I need. I have also looked here,

Thanks in Advance

Sounds like a tricky problem.

I've heard good things about the officer package for working with highly customized power-point presentations. I suspect you have similar options with read_docx()


You have mentioned all the options I know of. Although I haven't done so, I think you can use a Word template (which you could generate from the document handed to you) and then populate it accordingly via one or more of the methods you mention.

What particular capabilities are missing?


Attached is an example. Each of the plots are ggplot objects (inserted via copy and paste) and has an accompanying text box. The text box is the same size as the plot and it is aligned with the top of the plot object. These text boxes have to be editable. The example shows one panel, but the full document has 8 similar panels, all the same size and laid out the same way. (note, the example is a pdf, but the document produces has to be a docx file).

flextable.pdf (133.0 KB)

I don't think you attached the intended example?

Clear Creek.pdf (257.9 KB)

I would have thought you could do this with Word templates and officer, but if not, then I'm afraid I'm out of ideas.

I found a solution working with @davidgohel, the developer of officer and flextable. He suggested placing plots into a cell of a flextable and using the accompanying table cell as the location for text.
There was an issue with the resolution of the plots inside the flextable, but this has been fixed.
Thanks @davidgohel.
(the fixed code is available on his github).



1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.