Lays a data frame out in side-by-side blocks the same way gt_snake() lays
out the table, so a frame you computed against the long data still lines up
after the snake. It is the escape hatch for a precomputed grid you do not want
to rebuild as a condition: a logical mask for gt_highlight_cells(), a frame
of per-cell colors or text, anything the same shape as the snaked body.
Arguments
- x
A data frame or matrix with one row per row of the un-snaked table.
- n_cols
Integer. The number of blocks, matching the
gt_snake()call. Defaults to2.- rows_per_col
Integer. Rows in each block. Given this,
n_colsis worked out from the data instead, exactly asgt_snake()does it. Defaults toNULL.- fill
What to put in the trailing cells when the rows do not divide evenly. Defaults to
NA, whichgt_highlight_cells()reads as no match.
Details
Most of the time you do not need this. Body-cell styling applied before
gt_snake() is carried through the reshape on its own, so coloring the long
grid and then snaking already lands the fills in the right place. Reach for
this only when the grid lives outside the table's styles.
Pass the same n_cols (or rows_per_col) you passed to gt_snake(). Each
block's columns are suffixed with its block number, _1, _2, and so on, to
match the names gt_snake() gives the table. The result has
ceiling(nrow(x) / n_cols) rows and ncol(x) * n_cols columns, so it is the
shape gt_highlight_cells() expects for the block of snaked columns.
See also
gt_snake(), which reshapes the table itself and carries body
styling through.
Examples
if (FALSE) { # \dontrun{
library(gt)
wide <- head(mtcars[c("mpg", "hp", "wt", "qsec")], 8)
# a logical grid the same shape as the data, built before the snake
mask <- wide > 20
# after the snake, each column carries its block number as a suffix
snaked <- unlist(lapply(1:2, function(i) paste0(names(wide), "_", i)))
wide %>%
gt() %>%
gt_snake(n_cols = 2) %>%
gt_highlight_cells(tidyselect::all_of(snaked),
condition = gt_snake_align(mask, n_cols = 2),
fill = "#EDBD68")
} # }
