Looking at this relevant vignette:
With the code:
library(tidyverse)
library(knitr)
library(kableExtra)
library(formattable)
set.seed(1)
data.frame(letters = letters[1:5],
foo = rnorm(5, 20),
bar = rnorm(5, 20),
baz = rnorm(5, 20),
bash = rnorm(5, 20)) %>%
mutate(foo = color_tile("pink", "lightblue")(foo)) %>%
kable(escape = F) %>%
kable_styling("hover", full_width = F) %>%
column_spec(5, width = "3cm") %>%
add_header_above(c(" ", "Hello" = 2, "World" = 2))
You produce something like this:
Ultimately I would like to use the color_tile
function from formattable
rowwise - in my own dataset I need to compare values rowwise, while maintaining the structure of the data frame. I've tried transposing the dataframe but it makes a mess of my code and data and I don't think that route is plausible.
You can extract rows in a loop and run color_tile()
on them.
library(tidyverse)
library(knitr)
library(kableExtra)
library(formattable)
set.seed(1)
df <- data.frame(letters = letters[1:5],
foo = rnorm(5, 20),
bar = rnorm(5, 20),
baz = rnorm(5, 20),
bash = rnorm(5, 20),
stringsAsFactors = FALSE)
for(i in 1:nrow(df)) df[i,] <- color_tile("pink", "lightblue")(df[i,])
df %>%
kable(escape = F) %>%
kable_styling("hover", full_width = F) %>%
column_spec(5, width = "3cm") %>%
add_header_above(c(" ", "Hello" = 2, "World" = 2))