Skip to contents

Highlights missing cells and can relabel them in the same call. gt::sub_missing() substitutes text only, and only for real NA values.

Usage

gt_highlight_na(
  gt_object,
  columns = gt::everything(),
  fill = "#F0F0F0",
  text_color = NULL,
  bold = FALSE,
  italic = FALSE,
  missing_text = NULL,
  na_strings = "NA",
  ignore_case = FALSE,
  ...
)

Arguments

gt_object

A gt table object to modify.

columns

The column or columns to check. Defaults to all of them.

fill

Character. A hex color for the cell fill behind missing values. Defaults to "#F0F0F0".

text_color

Optional. A hex color for the text of missing values. Defaults to NULL.

bold

Logical. Should missing values be bolded? Defaults to FALSE.

italic

Logical. Should missing values be italicized? Defaults to FALSE.

missing_text

Optional. Replacement text for missing values, such as "--" or "Not reported". Defaults to NULL, which leaves the text alone.

na_strings

A character vector of strings to treat as missing alongside real NA. Defaults to "NA".

ignore_case

Logical. Should na_strings be matched case-insensitively? Defaults to FALSE.

...

Additional arguments passed to gt::cell_text.

Value

Returns a modified gt table with missing values styled.

Details

Alongside real NA, this also catches values that are literally the string "NA", a common artifact of reading a CSV and a frequent reason sub_missing() appears to do nothing. Widen na_strings to catch other placeholders such as "-" or "N/A".

See also

gt_outliers() for flagging values that are present but suspect.

Examples

library(gt)

# playoff_wins is NA for the teams that missed the playoffs
afc <- subset(
  sdv_example_standings, conference == "AFC",
  c(team_name, wins, losses, conference_rank, playoff_wins)
)

gt(afc) |> gt_highlight_na(playoff_wins)
team_name wins losses conference_rank playoff_wins
New England Patriots 14 3 2 3
Buffalo Bills 12 5 6 1
Miami Dolphins 7 10 10 NA
New York Jets 3 14 15 NA
Pittsburgh Steelers 10 7 4 0
Baltimore Ravens 8 9 9 NA
Cincinnati Bengals 6 11 11 NA
Cleveland Browns 5 12 13 NA
Jacksonville Jaguars 13 4 3 0
Houston Texans 12 5 5 1
Indianapolis Colts 8 9 8 NA
Tennessee Titans 3 14 16 NA
Denver Broncos 14 3 1 1
Los Angeles Chargers 11 6 7 0
Kansas City Chiefs 6 11 12 NA
Las Vegas Raiders 3 14 14 NA
# relabel as well as highlight gt(afc) |> gt_highlight_na(playoff_wins, missing_text = "missed playoffs", italic = TRUE, fill = "#FFF8E1" )
team_name wins losses conference_rank playoff_wins
New England Patriots 14 3 2 3
Buffalo Bills 12 5 6 1
Miami Dolphins 7 10 10 missed playoffs
New York Jets 3 14 15 missed playoffs
Pittsburgh Steelers 10 7 4 0
Baltimore Ravens 8 9 9 missed playoffs
Cincinnati Bengals 6 11 11 missed playoffs
Cleveland Browns 5 12 13 missed playoffs
Jacksonville Jaguars 13 4 3 0
Houston Texans 12 5 5 1
Indianapolis Colts 8 9 8 missed playoffs
Tennessee Titans 3 14 16 missed playoffs
Denver Broncos 14 3 1 1
Los Angeles Chargers 11 6 7 0
Kansas City Chiefs 6 11 12 missed playoffs
Las Vegas Raiders 3 14 14 missed playoffs
# also catch placeholder strings left behind by a CSV import gt(afc) |> gt_highlight_na(everything(), na_strings = c("NA", "N/A", "-"))
team_name wins losses conference_rank playoff_wins
New England Patriots 14 3 2 3
Buffalo Bills 12 5 6 1
Miami Dolphins 7 10 10 NA
New York Jets 3 14 15 NA
Pittsburgh Steelers 10 7 4 0
Baltimore Ravens 8 9 9 NA
Cincinnati Bengals 6 11 11 NA
Cleveland Browns 5 12 13 NA
Jacksonville Jaguars 13 4 3 0
Houston Texans 12 5 5 1
Indianapolis Colts 8 9 8 NA
Tennessee Titans 3 14 16 NA
Denver Broncos 14 3 1 1
Los Angeles Chargers 11 6 7 0
Kansas City Chiefs 6 11 12 NA
Las Vegas Raiders 3 14 14 NA