- 3 Minutes Wednesdays
- Posts
- 3MW (Data-Driven Powerpoint With R)
3MW (Data-Driven Powerpoint With R)
Guten Tag!
Many greetings from Munich, Germany. As you (hopefully) know, we’re on an {officer}
roll. The last two weeks we were talking about creating Word documents from within R. Today, I’m showing you how to create a data-driven slide deck using R and Powerpoint.
But first, it’s time for a few shameless plugs. You know, because I really want you to watch my videos 🥹🤪.
Text cleaning rollout has started
I know that a lot of you are also enjoying my data cleaning master class. So you might be glad to hear that Part 3 has started and I’ve upload the first set of three videos on text cleaining 🥳
We start with simple text cleaning techniques using functions like str_to_title()
and str_detect()
. But don’t worry, I will upload more videos on a regular schedule so that we will soon enough reach regex mastery.
Working with Databases
Have you ever tried to work with databases in R? With the {odbc}
and {DBI}
package that’s pretty straightforward. In my newest YouTube tutorial I show you how:
And now let’s get start with Powerpoints.
Prepare powerpoint template
First, we have to find a Powerpoint template that we want to use. You can either use my template or use one of your own. If you use your own, here’s a bit of prep work that you have to do with it:
Open the pptx-file with Powerpoint and switch to the slide master ( On the “View” tab, select “Slide Master”)
Throw out unnecessary layouts
Powerpoint will just name all of the placeholders “Textbox 1”, “Content 2”, etc. Do yourself a favor and rename those suckers into something you’ll recognize when you see it in R. You can use the alt + F10 shortkey to rename the IDs.
Delete all regular slides (outside of the slide master) and save your modified template
Read pptx file
Next, we can load the {officer}
package and read our template file.
Notice how this shows you all of the available layouts of the file. These were the things you were modifying in the slide master. As you can see, I’ve modified the “SlateVTI” master template that MS Office gave me so that it has only two different layouts.
Add a slide
Alright, let’s add a slide to our pptx document. To do so, we just have to pass our document to the add_slide()
function. There, we specify the layout and master template of the new slide.
Afterwards, we can have a look at our slide content.
Wow. Shocking. There’s nothing in that slide. But we know that there is a layout defined for that slide. You can see that with the layout_properties()
function.
Shoot! This shows ALL available layouts and the corresponding placeholders. Let’s filter this for the layout we’re currently interested in. And then we can focus on the labels.
Fill placeholders
Cool. Let’s insert texts where the placeholders Title-Text
and Subtitle-Text
are located. We can do that with the ph_with()
function.
This function takes two arguments:
the
"value"
argument: That can be a text, a data.frame, a flextable, a ggplot, you name it.the
"location"
argument: This is the place where to insert your content, duh. But you have to specify it using theph_location_label()
function in which you can use the IDs you’ve just seen.
Oh no! {officer}
seems to be unable to find the placeholders. Maybe, we have to explicitly tell it on what slide we’re currently on. We can to that with on_slide()
.
Nice. This worked perfectly. Well, maybe “perfectly” is a stretch. Let’s just say that we didn’t get an error and that’s nice.
Save document
So let’s throw this into an actual pptx
-file and see what’s what. If you’ve paid attention during the last couple of newsletter editions, then you know what to do.
Preparing another slide
Beautiful. Let’s add another slide. For that we can follow the same procedure. Let’s first add another slide.
Then, we can once again checkout what placeholders we have on this slide.
Let’s start with the basics. Let’s fill date
, footnote
, title
and slide_nmbr
.
Let’s check out intermediate result.
Adding flextables
Next, we can fill the remaining placeholder with a table. And as we’ve already learned, the {flextable}
package works wonderfully with the {officer}
package. So let’s create a flextable. (Don’t worry too much about the code if you haven’t worked with {flextable}
before.)
Then, we can put the table into the remaining placeholder and print the document into a file. Works just like last time.
Add a ggplot
By now, you probably get the idea. Still, let’s do one more example. You know, because no newsletter is complete without a ggplot. So first, we replicate all steps from before to create a new slide.
And then we can generate a ggplot and stick that into the slides just like with the flextable.
Hoooray 🥳 We made it. Time to celebrate and go touch some grass or something 🤪
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 in 2 week,
Albert 👋
Enjoyed this newsletter? Here are other ways I can help you:
Reply