Let’s use a portion of the countrypops dataset to create a gt table. We can relabel all the table’s columns with the cols_label() function to improve its presentation. In this simple case we are supplying the name of the column on the left-hand side, and the label text on the right-hand side.
Using the countrypops dataset again, we label columns similarly to before but this time making the column labels be bold through Markdown formatting (with the md() helper function). It’s possible here to use either a = or a ~ between the column name and the label text.
countrypops |> dplyr::select(-contains("code")) |> dplyr::filter( country_name =="Uganda", year %in%2017:2021 ) |>gt() |>cols_label(country_name =md("**Name**"),year =md("**Year**"), population ~md("**Population**") )
Name
Year
Population
Uganda
2017
40127085
Uganda
2018
41515395
Uganda
2019
42949080
Uganda
2020
44404611
Uganda
2021
45853778
With a select portion of the metro dataset, let’s create a small gt table with three columns. Within cols_label() we’d like to provide column labels that contain line breaks. For that, we can use <br> to indicate where the line breaks should be. We also need to use the md() helper function to signal to gt that this text should be interpreted as Markdown. Instead of calling md() on each of labels as before, we can more conveniently use the .fn argument and provide the bare function there (it will be applied to each label defined in the cols_label() call).
Using a subset of the towny dataset, we can create an interesting gt table. First, only certain columns are selected from the dataset, some filtering of rows is done, rows are sorted, and then only the first 10 rows are kept. After the data is introduced to gt(), we then apply some spanner labels using two calls of tab_spanner(). Below those spanners, we want to label the columns by the years of interest. Using cols_label() and select expressions on the left side of the formulas, we can easily relabel multiple columns with common label text. Note that we cannot use an = sign in any of the expressions within cols_label(); because the left-hand side is not a single column name, we must use formula syntax (i.e., with the ~).
Here’s another table that uses the towny dataset. The big difference compared to the previous gt table is that cols_label() as used here incorporates unit notation text (within "{{"/"}“}).
The illness dataset has units within the units column. They’re formatted in just the right way for gt too. Let’s do some text manipulation through dplyr::mutate() and some pivoting with tidyr::pivot_longer() and tidyr::pivot_wider() in order to include the units as part of the column names in the reworked table. These column names are in a format where the units are included within "{{"/"}“}, so, we can use cols_label() with the .process_units = TRUE option to register the measurement units. In addition to this, because there is a <br> included (for a line break), we should use the .fn option and provide the md() helper function (as a bare function name). This ensures that any line breaks will materialize.