Hi All,
I am putting together an interactive Rmd for a blog post I am writing but can't get kableExtra's cell_spec() to cooperate (rather, I may not be cooperating with it!). Simply, I would like to format the cells in a reactive table to be either green or red depending on two conditions. However, it only works for the first column i mutate, "DrugA" in this case. Here is a reproducible example:
my_df <- data.frame(
analysis = c("Expected Loss", "Proportion Cost Effective"),
DrugA = c(380, 0.55),
DrugB = c(300, 0.45)
) %>%
mutate(
DrugA = cell_spec(DrugA,
"html",
color = ifelse(DrugA < DrugB & analysis == "Expected Loss" | DrugA > DrugB & analysis == "Proportion Cost Effective",
"green",
"red")),
DrugB = cell_spec(DrugB,
"html",
color = ifelse(DrugB < DrugA & analysis == "Expected Loss" | DrugB > DrugA & analysis == "Proportion Cost Effective",
"green", "red"))
) %>%
kable("html", escape = FALSE) %>%
kable_styling(bootstrap_options = c("striped", "hover"))
Of course, if it were not reactive, I could print the html to the console and paste it within the Rmd; though the table needs to be reactive.
I have tried placing the cell_spec() in different formats... For example, I tried:
DrugA = ifelse(DrugA<DrugB & analysis == "Expected Loss" | DrugA>DrugB & analysis == "Proportion Cost Effective",
cell_spec(DrugA, "html", color = "green"),
cell_spec(DrugA, "html", color = "red")),
DrugB = ifelse(DrugB<DrugA & analysis == "Expected Loss" | DrugB>DrugA & analysis == "Proportion Cost Effective",
cell_spec(DrugB, "html", color = "green"),
cell_spec(DrugB, "html", color = "red"))
Thoughts?