Target cells in the table body and style accordinglySource:
tab_style_body() function we can target cells though value, regex,
and custom matching rules and apply styles to them and their surrounding
context (i.e., styling an entire row or column wherein the match is found).
Just as with the general
tab_style() function, this function is focused on
the application of styles for HTML output only (as such, other output formats
will ignore all
With the collection of
cell_*() helper functions available in gt, we
the background color of the cell (
the cell's text color, font, and size (
the text style (
style), enabling the use of italics or oblique text.
the text weight (
weight), allowing the use of thin to bold text (the degree of choice is greater with variable fonts)
the alignment and indentation of text (
the cell borders (
A table object that is created using the
a vector of styles to use. The
cell_borders()helper functions can be used here to more easily generate valid styles. If using more than one helper function to define styles, all calls must be enclosed in a
list(). Custom CSS declarations can be used for HTML output by including a
css()-based statement as a list item.
Optional columns for constraining the targeting process. Providing
everything()(the default) results in cells in all
columnsbeing targeting (this can be limited by
rowshowever). Can either be a series of column names provided in
c(), a vector of column indices, or a helper function focused on selections. The select helper functions are:
Optional rows for constraining the targeting process. Providing
everything()(the default) results in all rows in
columnsbeing targeted. Alternatively, we can supply a vector of row captions within
c(), a vector of row indices, or a helper function focused on selections. The select helper functions are:
everything(). We can also use expressions to filter down to the rows we need (e.g.,
[colname_1] > 100 & [colname_2] < 50).
The specific value or values that should be targeted for styling. If
patternis also supplied then
valueswill be ignored.
A regex pattern that can target solely those values in
character-based columns. If
valuesis also supplied,
patternwill take precedence.
A supplied function that operates on each cell of each column specified through
rows. The function should be fashioned such that a single logical value is returned. If either of
patternis also supplied,
fnwill take precedence.
A vector of styling target keywords to contain or expand the target of each cell. By default, this is a vector just containing
"cell". However, the keywords
"column"may be used separately or in combination to style the target cells' associated rows or columns.
A vector of locations to project styling. By default, this is a vector just containing
"body", whereby styled rows or columns (facilitated via inclusion of the
targets) will not permeate into the stub. The additional keyword
"stub"may be used alone or in conjunction with
"body"to project or expand the styling into the stub.
exibble to create a gt table with a stub and row groups. This
contains an assortment of values that could potentially undergo some styling
Cells in the table body can be styled through specification of literal values
values argument of
tab_style_body(). It's okay to search for
numerical, character, or logical values across all columns. Let's target the
33.33 and style those cells with an orange fill.
Multiple styles can be combined in a
list, here's an example of that using
the same cell targets:
gt_tbl %>% tab_style_body( style = list( cell_text(font = google_font("Inter"), color = "white"), cell_fill(color = "red"), cell_borders( sides = c("left", "right"), color = "steelblue", weight = px(4) ) ), values = c(49.95, 33.33) )
You can opt to color entire rows or columns (or both, should you want to)
with those specific keywords in the
targets argument. For the
we will style the entire row and with
33.33 the entire column will get the
gt_tbl %>% tab_style_body( style = cell_fill(color = "lightblue"), values = 49.95, targets = "row" ) %>% tab_style_body( style = cell_fill(color = "lightblue"), values = 33.33, targets = "column" )
In a minor variation to the prior example, it's possible to extend the
styling to other locations, or, entirely project the styling elsewhere. This
is done with the
extents argument. Valid keywords that can be included in
the vector are:
"body" (the default) and
"stub". Let's take the previous
example and extend the styling of the row into the stub.
gt_tbl %>% tab_style_body( style = cell_fill(color = "lightblue"), values = 49.95, targets = "row", extents = c("body", "stub") ) %>% tab_style_body( style = cell_fill(color = "lightblue"), values = 33.33, targets = "column" )
We can also use the
pattern argument to target cell values in
character-based columns. The
"fctr" column is skipped because it is in
fact a factor-based column.
For the most flexibility in targeting, it's best to use the
The function you give to
fn will be invoked separately on all cells so the
columns argument of
tab_style_body() might be useful to limit which cells
should be evaluated. For this next example, the supplied function should only
be used on numeric values and we can make sure of this by using
columns = where(is.numeric).
gt_tbl %>% tab_style_body( columns = where(is.numeric), style = cell_fill(color = "pink"), fn = function(x) x >= 0 && x < 50 )