Search code examples
rdplyrgt

How can I color every alternate table row in gt table in R


I want every other row in my gt table to be "grey". Is anyone aware of an already existing function

library(palmerpenguins)
library(gt)

penguins %>%
  dplyr::select(1:3) %>%
  dplyr::slice(1:10) %>% 
  gt()

enter image description here


Solution

  • We could use opt_stylize: The easiest way to style a gt table is to apply a pre-installed theme via opt_stylize().

    library(palmerpenguins)
    library(gt)
    
    penguins |>
      dplyr::select(1:3) %>%
      dplyr::slice(1:10) %>% 
      gt()|>
      opt_stylize(style = 1) 
     
    

    enter image description here