3MW (Creating Word Docs With R)

Guten Tag!

Many greetings from Munich, Germany. As much as I like R, I have to admit that the world runs on Microsoft Office. But that doesn’t mean that I have to transform my nice data analysis into a Word document by hand.

Thankfully, there’s the {officer} package. It helps you to wrap your data analysis into a report as a Word file. That way, your non-programming collaborators can take the report/analysis that you give them and adjust that according to what they need.

So today, I’m showing you how that works. But before we do that, time for a shameless plug for my newest YT video.

Text cleaning with R

As you know from last week’s newsletter, data can come in all kinds of forms. Sometimes it may even come in the form of a text file from which you have to extract the data yourself.

That’s where the {stringr} package comes in. And I’ve wrapped last week’s tutorial into a short video for you. You can check it out here 👇️ 

Create an empty word doc

In order to create Word documents with R, the first thing you have to do is to load the {officer} package and then execute the read_docx() function. This will create an empty Word doc within R for you.

Create a text

Then, you’ll want to create a bit of text. For example, we can add a timestamp to our document. This means that we assemble a text using the {glue} package and then wrap that into the ftext() function.

Add a paragraph to our document

Cool. We got a text so let’s add it to the body of our empty document. We can do that as part of a new paragraph via the body_add_fpar() function.

But since this will add a paragraph and not just a single line of text, we have to wrap our text into a paragraph. We do that with the fpar() function.

And in order to generate the actual Word document, we have to pass everything to the print() function where we specify the target file.

Add paragraph styling

Nice! That worked pretty smoothly. But it also looks boring as hell. Let’s try to add some style to this. We can modify

  • the text properties of the text inside the paragraph via fp_text() and

  • the paragraph properties via fp_par().

And then we just have to re-execute the printing command from before to get this result.

Changing text properties

Well, it looks like the paragraph styling changed nicely but the text styling didn’t take any effect. This can happen if there are some other default values set for the texts. The way to avoid that? Specify the properties inside of ftext().

Assemble paragraphs using differently formatted text

But you don’t have to use the same style on all of the text. You could even change the color of single words if you like. For that, all you have to do is use multiple ftext() calls inside of fpar().

And then if you rerun the printing code, you will see that only the included variables are formatted blue.

Add tables

Now we could add another paragraph with some text followed by a table. The paragraph could once again be added via body_add_fpar(). But for the table, we have to use body_add_table(). This function expects a data.frame/tibble that should be converted to a table.

And if you don’t like the style of the table, know that you can add any flextable you like. For that, you only need to make a flextable out of your data set and include that via body_add_flextable().

Add a ggplot

And to top it all off, you can add a ggplot to your word document as well. For that you will need a ggplot of course. But more importantly, you will need a body_add_gg() call.

If that isn’t a nice Word file I don’t know what is. Well, maybe I’m exaggerating a bit. But it’s a cool first demo of what the {officer} package has to offer. Next week, we’ll discover more things from the {officer} package.

As always, if you have any questions, or just want to reach out, feel free to contact me by replying to this mail or finding me on Linkedin.

See you next week,
Albert 👋

Enjoyed this newsletter? Here are other ways I can help you:

Reply

or to participate.