3MW (Using Word Templates With R)

Guten Tag!

Many greetings from Munich, Germany. Last week, I told you that the {officer} package let’s you build a Word document from scratch using R. But that was a bit much, wasn’t it?

Clearly, no one wants to create a WHOLE Word document from within R. It’s much easier to use Word to create a document template and then use the {officer} package to insert data-related things. So today, I’m showing you how to do that.

Find a template

First, let us download a template that we want to use. The cool thing about the {officer} package is that the look of the template doesn’t matter. We can even use one that uses fancy visual elements and styles. Like this one here:

Insert placeholder

Now that we have that template, we can modify it to our needs. For example, I want to show you how to create this Word document from the template:

So, a couple of quick modifications here and there and ready is our template:

Insert a date

As you can see, our template contains a few placeholders like “[DATE]”. These can be replaced using functions from the {officer} package. The key function to replace text is body_replace_all_text().

This function works by looking for a specific string and then replacing it with something else. Let’s try that with the “[DATE]” string.

Watch out! Regular expressions

Oh no. This doesn’t look right. Something weird is going on. And the weirdness has a name: It’s regular expressions.

You see, the thing that we stick into the old_value argument is a regular expression. This means it uses a complex but powerful language to describe patterns (btw, my data cleaning masterclass teaches that powerful language.)

In this language the brackets "[" and "]" have a special meaning. And that meaning is what destroys our text replacement. But we can tell that regular expression to use the brackets as is using "\\". That way, we get our desired result.

Insert an image

Nice! This worked pretty well. Now let’s try something harder. Let’s insert a company logo.

For that I have created a generic logo. It looks like this:

Unfortunately, there is no function to replace a text with an image. There is only body_add_img().

So in order to replace the “[COMPANY_LOGO]” placeholder with an image, we have to do three things:

  1. Navigate a fictitious cursor to the position of the paragraph that contains “COMPANY_LOGO”

  2. Remove the paragraph (as it only contains the placeholder)

  3. Add the image into that paragraph

And for all three of thoses steps there are three functions that do exactly that.

Right-align the image

Unfortunately, this doesn’t look nice. We want to have our image right-aligned. That’s why we have to make sure that the image is inserted in a right-aligned paragraph.

This can be done via the style argument in body_add_img(). But this argument requires the name of a pre-defined style. Hence, we have to define such a style using docx_set_paragraph_style() and the fp_par() function from last time.

And with that, we do have a nice right-aligned company logo.

Insert more space

Unfortunately, our date seems to have been moved by that. No problem. We just have to define the image paragraph style to have more padding at the bottom.

And once we have done that, we can reexecute the same code as before. Et voilà, we have a nicer date position.

Replace remaining placeholders

Next, let us replace all of the remaining placeholders as well. We use body_add_img() just like before. And just like it was the case with the date replacement, the style in which we formatted the placeholders always remains the same (even if they’re hideous.)

Insert ggplot

And to round it all off, let me show you how to insert a ggplot before the greetings. You only need to move the fictitious cursor again. This time, we move the cursor to the greetings and then insert the plot with body_add_gg().

Nice! This is a much nicer Word document compared to the one we’ve created last week. And it was much less work that we had to do from scratch. All it took was a bit of replacing stuff.

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.