How to set cell properties variably by cell contents
This help page explains how to set properties differently for cells, depending on their contents.
For example, in a table of p-values, you could bold cells where p < 0.05:
map_bold(pval_hux, by_ranges(0.05, c(TRUE, FALSE)))
Or you can use red text for a particular value:
hxtbl %>% map_text_color(by_values("Warning" = "red"))
There is a map_...
function for each huxtable cell property. The syntax is:
map_property(ht, row, col, fn)
where property
is the property name.
row
and col
specify ranges of rows and columns. See rowspecs for
details. To set properties for the whole table, omit row
and col
:
map_property(ht, fn)
The fn
argument is a mapping function which maps cell contents to
property values.
To set property values in "stripes" by rows or by columns, use by_rows()
and by_cols()
.
To set property values for cells with specific contents, use by_values()
.
To set property values for cells within a numeric range, use by_ranges()
.
To set property values for cells by quantiles, use by_quantiles()
or
by_equal_groups()
.
To set property values for cells that match a string or regular expression,
use by_regex()
.
To map numeric values to a colorspace, use by_colorspace()
.
For a more general solution, use by_function()
or by_cases()
.
Most functions convert the huxtable to a matrix using as.matrix()
. This can
have unexpected results if you mix character and numeric data. See the
example.
fn
takes four arguments: the entire original huxtable
ht
, a numeric vector of rows
, a numeric vector of cols
, and the
current
property values for ht[rows, cols]
, as a matrix. It should return
the new property values for ht[rows, cols]
, as a matrix.
ht <- hux(Condition = c("OK", "Warning", "Error")) ht <- map_text_color(ht, by_values( OK = "green", Warning = "orange", Error = "red" )) ht # Leaving NA values alone: map_text_color(ht, by_values( "OK" = "blue", NA, ignore_na = TRUE)) # Resetting values: map_text_color(ht, by_values( "OK" = "blue", NA, ignore_na = FALSE)) ht <- as_hux(matrix(rnorm(15), 5, 3)) map_background_color(ht, by_ranges( c(-1, 1), c("blue", "yellow", "red") )) map_background_color(ht, by_equal_groups(2, c("red", "green"))) ht <- hux( Coef = c(3.5, 2.4, 1.3), Pval = c(0.04, 0.01, 0.07), add_colnames = TRUE ) map_bold(ht, everywhere, "Pval", by_ranges(0.05, c(TRUE, FALSE))) # Problems with as.matrix: ht <- hux(c(-1, 1, 2), letters[1:3]) as.matrix(ht) # look at the spaces... as.matrix(ht) > 0 # uh oh map_text_color(ht, by_cases(. < 0 ~ "red", TRUE ~ "blue")) # To avoid this, only look at the truly numeric columns: map_text_color(ht, row = 1:3, col = 1, by_cases(. < 0 ~ "red", TRUE ~ "blue"))
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.