gt - Table


As there is so much to cover in this useful package, it’s best to consult the documentation page directly as gt() capabilities are countless and I cannot cover all in this document.

Consult the reference tab as well for more information.

Packages

#install.packages("gt")  install package in stand alone code
library(gt)

Create table


gt

gt(
  data,
  rowname_col = "rowname",
  groupname_col = dplyr::group_vars(data),
  process_md = FALSE,
  caption = NULL,
  rownames_to_stub = FALSE,
  row_group_as_column = FALSE,
  auto_align = TRUE,
  id = NULL,
  locale = NULL,
  row_group.sep = getOption("gt.row_group.sep", " - ")
)

The gt() function creates a gt table object when provided with table data. Using this function is the first step in a typical gt workflow. Once we have the gt table object, we can perform styling transformations before rendering to a display table of various formats.

gt_preview

gt_preview(data, top_n = 5, bottom_n = 1, incl_rownums = TRUE)

Sometimes you may want to see just a small portion of your input data. We can use gt_preview() in place of gt() to get the first x rows of data and the last y rows of data (which can be set by the top_n and bottom_n arguments). It’s not advised to use additional gt functions to further modify the output of gt_preview().

Modify table


tab_options

Modify the options available in a table: Reference

table_width

px & pct

You can pass numeric values in pixel or percent units. Severe weather effects case study.

impact_prop[1:10,] |>
        gt() |> 
        tab_header( title = md("**Property Damage Cost per Event**"),
                    subtitle = "10 most impactful events") |> 
        tab_options(table.align = "left", table.width = pct(50)) |> 
        fmt_number(suffixing = TRUE)

Helper functions


cell_txt

pct

A percentage value acts as a length value that is relative to an initial state. For instance an 80 percent value for something will size the target to 80 percent the size of its ‘previous’ value. This type of sizing is useful for sizing up or down a length value with an intuitive measure. This helper function can be used for the setting of font sizes (e.g., in cell_text()) and altering the thicknesses of lines (e.g., in cell_borders()). Should a more exact definition of size be required, the analogous helper function pct() will be more useful.

x |>
  gt() |>
  tab_style(
    style = cell_text(size = pct(75)),
    locations = cells_column_labels()
  )

Examples


Side by side

If we wanted to have two tables side by side follow this example from Severe weather case study.

  • We first create the two tables separately
  • Then we make one table out of the two
table_fatal <- impact_fatal[1:10,] |>
        gt() |> 
        tab_header( title = md("**Number of Fatalities per Event**"),
                    subtitle = "10 most impactful events") |> 
        tab_options(table.align = "left", table.width = pct(50))

table_injured <- impact_injured[1:10,] |>
        gt() |> 
        tab_header( title = md("**Number of Injuries per Event**"),
                    subtitle = "10 most impactful events") |> 
        tab_options(table.align = "left", table.width = pct(50))
tables <- data.frame(fatal=table_fatal, injured=table_injured)
tables |>
        gt() |>
        cols_label(
                fatal.EVTYPE = md("**Event**"),
                fatal.FATALITIES = md("**Fatalities**"),
                injured.EVTYPE = md("**Event**"),
                injured.INJURIES = md("**Injuries**")
                 ) |> 
        tab_header(title= md("**Event Type and Effect on Population of the U.S.**"),
                   subtitle = "10 most impactful events") |> 
        tab_options(table.align = "left", table.width = pct(70)) |> 
        cols_width("injured.EVTYPE" ~ px(100))