How to Add Headers to a Table Using formattable and kableExtra in R

Adding Headers to a Table using formattable in R

Introduction

In this article, we will explore how to add headers to a table in R using the formattable package. We will also discuss alternative approaches using kableExtra.

What is Formattable?

The formattable package is designed for creating nicely formatted tables with ease of use and customization options. It allows you to create tables quickly, making it an excellent choice for data analysts.

Creating a Table with Headers

To add headers to a table using the formattable package, we will first load the necessary libraries and import our dataset.

# Load required libraries
library(formattable)

# Import the mtcars dataset
data(mtcars)

Next, we create our dataframe from the mtcars dataset.

# Create a new dataframe
df <- mtcars

We will then pass our dataframe to the formattable function without any styling.

# Display the table
formattable(df)

The resulting output will be a simple table with no headers or styling.

Customizing the Table with Headers

However, we want a table that looks like this:

| | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |—-:|:—–:|:—-:|:—–:|:—:|:—–:|:—–:|—–:|:—–:|—–:|:—–:| | 1 | 21.0 | 6 | 160.0 | 110 | 3.90 | 2.76 | 18.0 | 1 | 4 | 4.0 | | 2 | 21.0 | 6 | 160.0 | 110 | 3.90 | 2.92 | 17.8 | 1 | 4 | 4.0 |

To add the headers, we can use a key/value pair vector with add_header_above from the kableExtra package.

Using kableExtra

Firstly, we need to load the necessary libraries.

# Load required libraries
library(kableExtra)
library(dplyr)

Next, we create our dataframe from the mtcars dataset.

# Create a new dataframe
df <- mtcars

We then use kable() with styling options.

# Display the table
mtcars %>%
  kable() %&gt;%
  kable_styling(bootstrap_options = "bordered",
                full_width = FALSE) %&gt;%
  add_header_above(c("", "Category1" = 3, "Category2" = 3,
                     "Category3" = 3, "Category4" = 2)) %&gt;%
  collapse_rows(columns = 1, valign = "middle")

The kable() function allows us to create a nicely styled table. The bootstrap_options parameter is used for styling the table, and we use full_width to ensure the table fits in the viewer.

Customizing the Table with kableExtra

We can further customize our table by changing some of the styling options.

# Display the table
mtcars %>%
  kable() %&gt;%
  kable_styling(bootstrap_options = "responsive",
                full_width = FALSE) %&gt;%
  add_header_above(c("", "Category1" = 3, "Category2" = 3,
                     "Category3" = 3, "Category4" = 2)) %&gt;%
  collapse_rows(columns = 1, valign = "middle")

We can make our table responsive by changing the styling options to "responsive".

Understanding Key/Value Pairs

Let’s understand what we did here. We used a key/value pair vector in add_header_above where:

  • The first element of the vector is an empty string (""), which means that the corresponding column will be skipped.
  • Each subsequent element is a string representing the name of a column, followed by its size.

For example, "Category1" = 3 means that we want to make the header for the “mpg” column appear three rows above the rest. The add_header_above() function takes a vector with key/value pairs where:

  • Key is the name of the header.
  • Value is the size of the row.

For example, "Category1" = 3 means that we want to make the “mpg” column appear three rows above the rest.

Understanding Add_header_above

The add_header_above() function allows us to add headers at specific positions in our table. Here’s a more detailed explanation of how this works:

  • The first element is an empty string (""), which means that no row will be added.

  • For example, "Category1" = 3 means that we want the “mpg” column to appear three rows above the rest.

Understanding Collapse Rows

The collapse_rows(columns = 1, valign = "middle") function helps in aligning the header correctly. Here’s how it works:

  • The columns = 1 parameter is used to specify which column we want to collapse.

  • The valign = "middle" parameter is used to center the text vertically.

Understanding Styling Options

The styling options for kable() are as follows:

  • bootstrap_options: It allows us to choose from several pre-designed styles. Here, we chose "bordered".
  • full_width: It makes the table full width.

Here is an example of how to change this:

# Display the table
mtcars %>%
  kable() %&gt;%
  kable_styling(bootstrap_options = "responsive",
                full_width = FALSE) %&gt;%
  add_header_above(c("", "Category1" = 3, "Category2" = 3,
                     "Category3" = 3, "Category4" = 2)) %&gt;%
  collapse_rows(columns = 1, valign = "middle")

The bootstrap_options parameter is used to choose from several pre-designed styles. Here, we chose "responsive".

Conclusion

Using the key/value pairs and collapsing rows helps in aligning the header correctly.

We also learned how to change some of the styling options to make our table responsive or full width.


Last modified on 2024-04-05