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:

Or you can use red text for a particular value:

There is a map_... function for each huxtable cell property. The syntax is:

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:

The fn argument is a mapping function which maps cell contents to property values.

Caveat

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.

Technical details

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.

Examples

ht <- hux(Condition = c("OK", "Warning", "Error"))
ht <- map_text_color(ht, by_values(
        OK      = "green",
        Warning = "orange",
        Error   = "red"
      ))
ht
#>                                  Condition       
#>                                  OK              
#>                                  Warning         
#>                                  Error           
#> 
#> Column names: Condition

# Leaving NA values alone:
map_text_color(ht, by_values(
      "OK" = "blue", NA, ignore_na = TRUE))
#>                                  Condition       
#>                                  OK              
#>                                  Warning         
#>                                  Error           
#> 
#> Column names: Condition

# Resetting values:
map_text_color(ht, by_values(
      "OK" = "blue", NA, ignore_na = FALSE))
#>                                  Condition       
#>                                  OK              
#>                                  Warning         
#>                                  Error           
#> 
#> Column names: Condition

ht <- as_hux(matrix(rnorm(15), 5, 3))
map_background_color(ht, by_ranges(
        c(-1, 1),
        c("blue", "yellow", "red")
      ))
#>                             -0.531    1.36     0.948  
#>                             -1.05    -0.939   -0.806  
#>                             -0.902    0.302    1.61   
#>                              1.21     0.16    -0.158  
#>                              0.645    0.562    0.761  
#> 
#> Column names: V1, V2, V3
map_background_color(ht,
      by_equal_groups(2, c("red", "green")))
#>                             -0.531    1.36     0.948  
#>                             -1.05    -0.939   -0.806  
#>                             -0.902    0.302    1.61   
#>                              1.21     0.16    -0.158  
#>                              0.645    0.562    0.761  
#> 
#> Column names: V1, V2, V3

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)))
#>                                   Coef      Pval  
#>                                    3.5      0.04  
#>                                    2.4      0.01  
#>                                    1.3      0.07  
#> 
#> Column names: Coef, Pval

# Problems with as.matrix:

ht <- hux(c(-1, 1, 2), letters[1:3])
as.matrix(ht)          # look at the spaces...
#>      c(-1, 1, 2) c("a", "b", "c")
#> [1,] "-1"        "a"             
#> [2,] " 1"        "b"             
#> [3,] " 2"        "c"             
as.matrix(ht) > 0      # uh oh
#>      c(-1, 1, 2) c("a", "b", "c")
#> [1,]       FALSE             TRUE
#> [2,]       FALSE             TRUE
#> [3,]       FALSE             TRUE
map_text_color(ht,
      by_cases(. < 0 ~ "red", TRUE ~ "blue"))
#>                                     -1   a        
#>                                      1   b        
#>                                      2   c        
#> 
#> Column names: c(-1, 1, 2), c("a", "b", "c")

# 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"))
#>                                     -1   a        
#>                                      1   b        
#>                                      2   c        
#> 
#> Column names: c(-1, 1, 2), c("a", "b", "c")