I am trying to highlight the top four lines of the tables (One, followed by No 3 (50%) through Yes (2 33%). My actual table is bigger and I am highlighting every other variables with the corresponding levels. I can get the variable name to highlight but cannot get the levels to highlight, and there are some instances where I might have the same text and numbers for these levels as you can see in my reprex.
library(gt)
library(gtsummary)
tribble(
~One, ~Two,
"No", "Yes",
"Yes", "Yes",
"No", "No",
"Unknown", "Yes",
"No", "Unknown",
"Yes", "Yes"
) %>%
tbl_summary(digits = all_categorical() ~ 0) %>%
modify_header(
update = list(stat_0 ~ md(c("**Patients**")))) %>%
as_gt() %>%
tab_header(
title = md(c("**Table 5.**","Description"))) %>%
tab_style(
style = list(
cell_fill(color = "#e0ecf4")),
locations = cells_body(
columns = vars("label","stat_0"),
rows = label == c("One"))) %>%
tab_style(
style = list(
cell_fill(color = "#e0ecf4")),
locations = cells_body(
columns = vars("label","stat_0"),
rows = label == c("3 (50%)"))) %>%
tab_style(
style = list(
cell_fill(color = "#e0ecf4")),
locations = cells_body(
columns = vars("label","stat_0"),
rows = label == c("1 (17%)"))) %>%
tab_style(
style = list(
cell_fill(color = "#e0ecf4")),
locations = cells_body(
columns = vars("label","stat_0"),
rows = label == c("Yes 2 (33%)")))
Here's a code example of how to highlight every other variable from a gtsummary table.
library(gt)
library(gtsummary)
# build gtsummary table
tbl <-
trial %>%
select(age, marker, grade) %>%
tbl_summary()
# save vecotr of every other variable
every_other_variable <- unique(tbl$table_body$variable)[c(T, F)]
every_other_variable
#> [1] "age" "grade"
# highlight every other row
tbl_highlighted <-
as_gt(tbl) %>%
tab_style(
style = list(cell_fill(color = "#e0ecf4")),
locations = cells_body(rows = variable %in% every_other_variable)
)