3MW (Creating interactive tables with {reactable})

Guten Tag!

Many greetings from Munich, Germany. As we’ve discussed last week, the R programming language has a rich ecosystem of packages that are fantastic for creating beautiful production-grade tables from within R.

Today, I’m showing you that one package that makes it really easy (mostly) to create interactive tables. Namely, I’m going to show you {reactable}. 🥳 

What can {reactable} do for me?

The {reactable} package is the most convenient way to make tables that are interactive. You know, the tables that are filterable and sortable. That kind of stuff.

Unfortunately, the syntax isn’t as nice as the one from {gt} and sometimes you have to do a bit of JavaScript. But still, it’s a a really great package that can do a whole lot for you out of the box.

Mini Tutorial

Fake data

Let’s create a similar table that we have created last week. This time, we’re making it interactive with the {reactable} package. But we can still use the same data as last week. Nothing stops us from that:

Base Layer

To create a table all we have to to is to pass the data to the reactable function.

The nice thing is that this is interactive out of the box. The table is paginated and by clicking onto the column names, you can sort the rows.

Use better column names

Unlike {gt}, the {reactable} package doesn’t allow you to change the table step by step by chaining pipes. Instead, you will have to use one of the many arguments of reactable() and helper functions to get things done.

For example, to set nicer column names you can use the columns argument with a list of column definitions (using the colDef() helper)

Title & Subtitle

For adding a nice title and subtitle to your plot, you can either use some custom HTML & CSS tricks or you just use the {reactablefmtr} package.

For some reason, the RStudio viewer doesn’t show the pagination anymore. But if you open the table in your browser, everything is still there.

Format numbers

The numbers in the revenue column correspond to dollar amounts. We can format them by specifying a column format inside of colDef() with help from the colFormat() helper function.

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. The cool thing about {reactable} is that it’s really easy and the output becomes nicely interactive out of the box. All you have to do is set the groupBy argument.

Add summaries

You can add group summaries by using the aggregate argument inside of colDef() and setting it to one of the built-in aggregate functions like "mean" or "sum". If you want to do something custom, you can do that, but then you will have to write a custom JavaScript function for that.

Make table filterable

If we wanted to make our table more interactive, we could make the month column filterable. That way, we can look for particular columns. In that case, it probably makes sense to have the groups unfolded by default.

Change row styling

Finally, to add a little bit more style and visual structure let us make the group rows blue. For that, we need to write a JavaScript function that takes the rowInfo object as an argument and returns a JSON object with camelCased style properties.

More Resources

Nice! With that we have learned the basic principles of {reactable}. If you want to learn more about {reactable}, you may want to check out the fantastic {reactable} cookbook that gives you soooo many examples that you can adjust to what you need:

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.