I created a table using the dplyr package, but I want to color-code the resulting median values based on the range shown in the formatting step. This worked, but the formatted result is HTML for reactable and I want the dataframe itself color-coded so that I can export to Excel (with formatting ). I've searched for a solution for several days now, and resulted in nothing but HTML results .
This table has years going across, so I'd prefer to use a vector and not have to write a condition for each one.
#>I want to format the results of stateYR created below
library(dplyr)
library(tidyr)
stateYR <- DF %>%
group_by(state, YR) %>%
summarize(ratio = median(ratio , na.rm = TRUE)) %>%
mutate_if(is.numeric, round, 2) %>%
pivot_wider(id_cols = state, names_from = YR, values_from = ratio, values_fill = list(ratio = 0))
library(reactable)
library(tidyverse)
columns <-
stateYR %>%
colnames() %>%
set_names() %>%
keep(~ .x %in% colnames(stateYR)[2:15]) %>%
map(~ {
colDef(
style = function(value) {
ds_color <- ifelse(value < 0, "red",
ifelse(value >=0 & value <0.05, "green",
ifelse(value >=.05 & value <.1, "coral",
ifelse(value >= .1, "red"))))
list(background = ds_color)
}
)
})
reactable(stateYR, columns = columns)
results <-reactable(stateYR)