Change the style of a column or single cells within columns.
Usage
style_column(
tbl,
columns = dplyr::everything(),
rows = NULL,
background_color = NULL,
text_color = NULL,
font_size = NULL,
bold = FALSE,
italic = FALSE,
color_scale = NULL,
openxlsx_style = NULL,
gt_style = NULL,
hux_style = NULL,
flex_style = NULL,
stack = TRUE
)Arguments
- tbl
tablespan table
- columns
the columns to style. Must be a tidyselect selector expression (e.g., starts_with("hp_"))
- rows
indices of the rows which should be styled. When set to NULL, the style is applied to all rows
- background_color
hex code for the background color
- text_color
hex code for the text color
- font_size
font size
- bold
set to TRUE for bold
- italic
set to TRUE for italic
- color_scale
a named vector of length 2 or 3 to define a color scale. Example for two colors: color_scale = c("#EE2F43" = -1, "#37E65A" = 1). Example for three colors: color_scale = c("#EE2F43" = -1, "#FFFFFF" = 0, "#37E65A" = 1). If a value is set as NA, it will be replaced with the minimum, mean, or maximum respectively (e.g., color_scale = c("#EE2F43" = -1, "#FFFFFF" = 0, "#37E65A" = 1) will be replaced by color_scale = c("#EE2F43" = min(data), "#FFFFFF" = 0, "#37E65A" = max(data))).
- openxlsx_style
optional custom openxlsx style. When provided, all other arguments are ignored
- gt_style
optional custom gt style. When provided, all other arguments are ignored
- hux_style
optional custom huxtable style. When provided, all other arguments are ignored. Must be a function with the following signature: function(tbl, row, col){apply some style to the table and return the table}. Example: function(tbl, row, col){tbl |> huxtable::set_bold(row = row, col = col)}
- flex_style
optional custom flextable style. When provided, all other arguments are ignored. Must be a function with the following signature: function(tbl, row, col, part){apply some style to the table and return the table}. Example: function(tbl, row, col, part){tbl |> flextable::color(i = row, j = col, color = "red", part = part)}
- stack
When set to TRUE, the style is added on top of the existing styles. This is mostly relevant for openxlsx. When set to FALSE, the new style replaces all previous styling.
Examples
library(tablespan)
library(dplyr)
data("mtcars")
# We want to report the following table:
summarized_table <- mtcars |>
group_by(cyl, vs) |>
summarise(N = n(),
mean_hp = mean(hp),
sd_hp = sd(hp),
mean_wt = mean(wt),
sd_wt = sd(wt))
#> `summarise()` has grouped output by 'cyl'. You can override using the `.groups`
#> argument.
# Create a tablespan:
tbl <- tablespan(data = summarized_table,
formula = Cylinder:cyl + Engine:vs ~
N +
(`Horse Power` = Mean:mean_hp + SD:sd_hp) +
(`Weight` = Mean:mean_wt + SD:sd_wt),
title = "Motor Trend Car Road Tests",
subtitle = "A table created with tablespan",
footnote = "Data from the infamous mtcars data set.")
if(require_gt(throw = FALSE))
tbl |>
style_column(columns = mean_hp,
bold = TRUE) |>
as_gt()
Motor Trend Car Road Tests
A table created with tablespan
Mean
SD
Mean
SD
Data from the infamous mtcars data set.