I am attempting to highlight the text of a KableExtra table to indicate where group observations increase or decrease by year. However, I am receiving an error about attempting to replicate a closure. I understand that the issue may be with the function that I created, but apart from this issue; in other attempts, the output would indicate that nothing in the ifelse statement is true.
set.seed(1)
# create tibble
test <- tbble(year = c(replicate(3, c(2010, 2015, 2020))), # replicate 2010, 2015, and 2020 three times
location = c(replicate(3, c(1, 2, 3))),
a = sample(0:500000, 9), # random sample of 9 numbers
b = sample(0:500000, 9),
c = sample(0:500000, 9),
d = sample(0:500000, 9)) %>%
mutate(across(1:2, as_factor)) %>%
# create table
kbl("html", # table output
escape = F) %>% # do not escape
kable_material_dark() # use dark theme
# column_spec across test [, 3:6]
# color with function(x)
column_spec(test, 3:6, color = function(x) {
ifelse(isTRUE(x > dplyr::lag(x)), "green", # increased numbers = green
ifelse(isTRUE(x < dplyr::lag(x)), "red", x)) # decreased = red, no decrease/increase remains as is
})
Shaq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.