gtsummary --> gt --> tab_style()

Hi! I am trying to leverage gt styling for a gtsummary table.

Here is an example:

library(gtsummary)
library(gt)

trial |>
  select(trt, grade) |>
  tbl_summary(
    by = trt
  ) |> 
  as_gt() |> 
  tab_style(
    style = list(
      cell_fill(color = "#FFFBC8")
    ),
    locations = cells_body(rows = 1)
  )

where the entire first row gets higlighted.

However, I would only like for the first cell to be highlighted, not the entire row (so only the word Grade). I have tried a couple of options with the cells_body function, including the columns argument specified by name or location, but I can't figure it out. Any help would be appreciated!

1 Like

Hi @shannon.pileggi , remember put a reproducible example of data.

See this reprex

With a toy data, Im make this example:

library(gtsummary)
library(gt)

data <- data.frame(
  trt = c("A", "A", "B", "B"),
  grade = c("Grade1", "Grade2", "Grade1", "Grade2"))

summary_table <- data|>
  select(trt, grade) |>
  tbl_summary(by = trt) |>
  as_gt() 

summary_table

styled_table <- summary_table |>
  tab_style(
    style = list(
      cell_fill(color = "#FFFBC8")),    
      locations = cells_body(
      rows = 1,
      columns = c("label"))) # Assuming "label" is the correct column name

styled_table

image

1 Like

Ah, thank you so much! That did it! I had trouble figuring outwhat the expected column name was, so I really appreciate it.

FWIW, the example initially provided is reproducible as the trial data is in the gtsummary package. Nonetheless, I do really appreciate the assistance!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.