I have the following dataframe:
library(knitr)
library(kableExtra)
library(dplyr)
df<- structure(list(date = c("2021-04-12", "2021-04-13", "2021-04-14","2021-04-15", "2021-04-16", "Avg", "Change", "Change (%)"),
Audi = c(3.3, 3.7, 6.6, 6.9, 6.8, 6.3, 0.5, 5.5),
Bmw = c(1.1, 1.7, 4.1, 4.2, 4, 3.4, -2.5, -4.1), Lexus = c(9.8, 0.3,2.3, 2.2, 2, 1.4, 1.9, 3.2)), row.names = c(NA, -8L), class = "data.frame")
I wanted to highlight rows that satisfy the condition df$date == c("Change", "Change (%)")
in specific columns using this code. Additionally I want to highlight with gray colour the row which(df$date == "Avg")
for all columns except column date
.
df %>%
kable() %>%
kable_classic(full_width = F, html_font = "Calibri") %>%
row_spec(which(df$date == "Avg"), bold = T, background = "#F2F2F2") %>%
row_spec(which(df$date == c("Change", "Change (%)")), bold = T, background = ifelse(df$Audi >= 0, "#C6EFCE", "#FFC7CE"))
I found a little bit similar question here but did not figure out how to implement it in my code
How can I highlight the rows which(df$date == c("Change", "Change (%)")) for all columns except date (condition: positive values are green and negative are red)
How can I highlight with gray color the rows which(df$date == c("Avg")) for all columns except date
I also found here that I can change the colour of all values in a table adding
mutate_all(~cell_spec(.x, color = ifelse(.x < 0, "red"," green")))
, however got an error
Error in UseMethod("tbl_vars") : no applicable method for 'tbl_vars' applied to an object of class "c('kableExtra', 'knitr_kable')"
How can I solve these issues?