在 gt 中发出更改单元格颜色的问题

Issue changing cell color in gt

我试图突出显示 table 的前四行(第一行,然后是第 3 行(50%)到是(2 33%)。我的实际 table 更大我正在突出显示具有相应级别的所有其他变量。 我可以获得要突出显示的变量名称,但无法获得要突出显示的级别,并且在某些情况下,这些级别的文本和数字可能与您在我的 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%)")))

这是一个代码示例,说明如何突出显示 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)
  )