I have a table that shows data in two different units across 12 months for 4 different channels. I need that the first column, which contains the channel information, stays in place when I scroll to the right, so I can see what information I’m looking at. However, after I fixate the first column, it completely ignores the background-color argument, but not the color, which makes it completely white. I need it to have the same color as the rest of the header.
This is how it looks
This is how I need it to be
This is my code
fix_doubled_months <- function(month){
month_1 <- str_extract(month, "[0-9]{6}")
month_final <- character(length(month))
month_final[str_detect(month, "dollar")] <- month_1[str_detect(month, "dollar")]
month_final[str_detect(month, "lbs")] <- paste0(" ", month_1[str_detect(month, "lbs")], " ")
return(month_final)
}
distribution_summary <- tibble(
date = rep(seq(from = as.Date("2023-01-01"), length.out = 12, by = "+1 month"), times = 8),
channels = rep(rep(paste0("Channel ", 1:4), each = 12), 2),
value = round(rnorm(96, 500, 100), 2),
unit = rep(c("lbs", "dollars"), each = 48)
) |>
mutate(date = format(date, format = "%Y%m"),
date_unit = paste0(date, "_", unit)) |>
select(-c(date, unit)) |>
pivot_wider(names_from = date_unit, values_from = value) |>
rename_at(vars(matches("^[0-9]{6}_")), fix_doubled_months)
container_summary <- withTags(table(
class = "display",
thead(
tr(
th(rowspan = 1, ""),
th(colspan = (length(distribution_summary) - 2)/2, "Lbs"),
th(colspan = (length(distribution_summary) - 2)/2, "$"),
),
tr(
lapply(names(distribution_summary), th)
)
)
))
distribution_summary |>
datatable(
rownames = FALSE,
extensions = "FixedColumns",
container = container_summary,
options = list(fixedColumns = list(leftColumns = 1),
scrollX = TRUE,
scrollY = FALSE,
dom = "t",
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#4F2170', 'color': '#FFFFFF', 'white-space': 'nowrap'});",
"}"
))) |>
formatStyle(names(distribution_summary), "white-space" = "nowrap") |>
formatStyle("channels", backgroundColor = "#4F2170", color = "#FFFFFF", fontWeight = "bold")

