You can modify spec_color and spec_font_size functions in order to work on rows instead of columns (just type F2 in RStudio to get their original source code) :
# Define row colors
spec_color_row <- function (x,
rowmin,
rowmax,
alpha = 1,
begin = 0,
end = 1,
direction = 1,
option = "D",
na_color = "#BBBBBB")
{
x <- pmin(round((x - rowmin) / (rowmax - rowmin) * 255) + 1, 256)
color_code <- viridisLite::viridis(256, alpha, begin, end,
direction, option)[x]
color_code[is.na(color_code)] <- na_color
return(color_code)
}
# Define row font sizes
spec_font_size_row <- function (x,
rowmin,
rowmax,
begin = 8,
end = 16,
na_font_size = 12)
{
x <- pmin(round((end - begin) * (x - rowmin) / (rowmax - rowmin)) + begin, end)
x[is.na(x)] <- na_font_size
return(x)
}
After this, you should define the columns you want to use and calculate the maximum and minimum for each row.
In the example below all numeric columns are used :
iris_cols <- iris %>% select_if(is.numeric) %>% names()
data <- iris %>% mutate(rowmax = pmax(!!!rlang::syms(iris_cols)),
rowmin = pmin(!!!rlang::syms(iris_cols)))
Then you can use mutate and across to calculate font size & color.
For this to work you'll need to install dplyr >= 1.0.0
data %>% mutate(across(iris_cols,
~cell_spec(., bold = T,
color = spec_color_row(.,rowmin, rowmax, end = 0.9),
font_size = spec_font_size_row(.,rowmin ,rowmax)))) %>%
kable(escape = F, align = "c") %>%
kable_styling(c("striped", "condensed"), full_width = F)
EDIT : following your last question, this can be further improved/compacted using rowwise and the new c_across function :
iris[1:10,] %>% rowwise() %>%
mutate(rowmin = min(c_across(is.numeric)),
rowmax = max(c_across(is.numeric))) %>%
mutate(across(is.numeric,
~cell_spec(., bold = T,
color = spec_color_row(.,rowmin, rowmax, end = 0.9),
font_size = spec_font_size_row(.,rowmin ,rowmax)))) %>%
select(-rowmin,-rowmax) %>%
kable(escape = F, align = "c") %>%
kable_styling(c("striped", "condensed"), full_width = F)
However I didn't yet manage to get fully rid of rowmin / rowmax intermediate calculations, because column manipulations are easier as row manipulations in dplyr. That's why I liked @dww solution to transpose the dataframe to overcome this difficulty.
