Katherine Anderson

By Katherine Anderson

Extension of Previous Tabsets Tutorial

A previous tutorial, “How to use tabsets in R Markdown”, introduced tabsets for grouping figures together into an interactive instance. In an R markdown notebook, users can define the tab labels with markdown code and write R code blocks between the labels for the figures that are displayed under each label.

This can be cumbersome if there are many tabs desired and code blocks
that need to be written under each label. It is also difficult if the desired tab labels come from data or parameters calculated within the notebook, and the user would need to manually figure out the labels and write them in markdown code.

Dynamic tabs can be programmatically generated, which allows users to group figures as defined by the data and reduce code clutter within the R markdown notebook as only one code block is needed rather than a separate code block for each tab. Using the diamonds dataset from the R package ggplot2, this post will show how to dynamically create tabs in an R markdown notebook.

Example of Tabs Created Manually

Suppose you would like to separate your data into groups and create similar figures for each group to display in a notebook. This could be done by manually figuring out the groups by looking at the data, then writing markdown tabs and code blocks for each group. The manual markdown tabs do not allow for the notebook to run potential new data with any new groups that appear. Below is an example of this issue where the notebook plots diamond price per carat for each cut group using tabsets.

Comparing Diamond Prices by Carat Across Different

Cut Qualities

Fair

Good

Very Good

Premium