- 3 Minutes Wednesdays
- Posts
- 3MW (Bring tables to Word & Powerpoint with {flextable})
3MW (Bring tables to Word & Powerpoint with {flextable})
Guten Tag!
Many greetings from Munich, Germany. Are you done with tables yet? I hope not. The R ecosystem has more to offer.
Over the last two weeks, we have covered the fantastic {gt}
and {reactable}
packages. Both are excellent at what they do.
{gt}
has an impeccable workflow to create static tables and {reactable}
is the ultra-champ at creating interactive tables. Today, we get to know another champion. Namely, {flextable}
.
But before we do that that, time for two short little announcement:
Data Cleaning Master Class Part 2
I’m about to release Part 2 of my Data Cleaning Master Class. Right now, I’m in the midst of final video editing but then next week, I’ll release the next wave of data cleaning tutorials. 🥳 Part 2 of the course will
cover a myriad of data formats,
show you how to read them into R and
clean them so that you can work with that data.
I’m particularly proud to say that I do cover how to clean up Excel files as well (incl. pivot tables). So next time your coworker sends you an Excel file, you definitely know how to ingest and work with that data gracefully.
And finally a quick heads-up: Next week, the price of the course will go up to $185. So if you want to save some money on that, now’s your chance. You can get the course at
{reactable}
video tutorial
Looking for a video version of last week’s newsletter? Well, look no further. I’ve created a video intro on {reactable}
with a couple more features that we didn’t even cover in the newsletter. Check it out on Youtube:
Now, let’s dive into this week’s issue.
What can {flextable}
do for you?
The biggest feature of {flextable}
is that it has super strong export capabilities to Microsoft Office. You know, pesky little files like Word documents or Powerpoint slides. Thus, if you frequently find yourself copying a data table over to Word or Powerpoint, then look no more. Let {flextable}
handle that for you.
Mini Tutorial
Fake data
Let’s create a similar table that we have created in the last two weeks. By now, you probably know the data set by heart.
Base Layer
Just like with all the other table packages, the first step is really simple. Just throw your data into the flextable()
function.
Use better column names
Now, let’s do better column names again. Let’s go for title-case. And the nice thing with {flextable}
is that it allows a pipe-able code style like {gt}
.
Title & Subtitle
For adding a nice title and subtitle to your plot, you can add so-called header lines.
Format numbers
The numbers in the revenue column correspond to dollar amounts. We can format them by specifying a column format with one of the colformat_*()
functions. Unfortunately, there’s no such thing as colformat_currency()
so we’ll have to improvise with colformat_double()
.
Add groups
Now, I want to structure my tables into quarters. The easiest way to do that is to use the quarter column in our data set for grouping.
But flextable()
doesn’t allow to do that inside of the function. Instead one has to pass in the data as a grouped data set.
Add summaries
For adding summaries to a table, {flextable}
is actually one of the trickiest packages. You will have to calculate totals yourself and assemble a new data set.
Then, you can throw that into flextable()
again.
Change row styling
To add a little bit more style and visual structure, let us make the group rows blue and the totals bold. For that, you can use the bg()
and bold()
functions to apply the styles. And to target specific rows, you need to set i
to a function (using the ~
notation) that evaluates to TRUE
s and FALSE
s.
Export
Finally, we can make use of {flextable}
's fantastic export capabilities and export to Word…
…or to Powerpoint (though you might want to do that with a table that fits into a rectangular slide)
More Resources
Nice! With that we have learned the basic principles of {flextable}
. If you want to learn more about this package, then one of my video courses dives into many details that will help you create beautiful tables. You can check it out at:
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