Home > OS >  How to fill the maximum value between two different columns in gt table?
How to fill the maximum value between two different columns in gt table?

Time:10-20

I have this small data set and I'd like to apply a fill and change the text colour to white in the cell with the largest value between the first (team_1) and third (team_2) columns. How can I do this in gt? For example, 25.9 should be filled blue and coloured white, while 17.6 is left alone an so on. I presume it will have something to do with tab_options(). Thanks.

library(gt)

dat <- data.frame(
  team_1 = c(17.6, 33, 6),
  stat = c("stat1", "stat2", "stat3"),
  team_2 = c(25.9, 28, 8)
)

CodePudding user response:

You can do:

library(gt)

dat %>%
  gt() %>%
  tab_style(
    style = list(cell_fill(color = "blue"),
                 cell_text(color = "white")),
    locations = cells_body(columns = team_1,
                           rows = team_1 > team_2)
  ) %>%
  tab_style(
    style = list(cell_fill(color = "blue"),
                 cell_text(color = "white")),
    locations = cells_body(columns = team_2,
                           rows = team_2 > team_1)
  )

enter image description here

  •  Tags:  
  • rgt
  • Related