Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to format gt table across a single row?

I'm trying to format my gt table with conditions and am at a bit of a standstill. Most of the examples I'm finding are formatting based off of column headers.

Here's the code for an example table with the first column named "row" w/ values 1-6. How would i get the gt table to add a color ONLY to values > 3.0 across row 1? This is my first question, apologies if I messed something up!

Here's how I'd like the table to look like.

library(tidyverse)  
library(gt) 

iris %>%   
  group_by(Species) %>%   
  slice_max(Sepal.Length, n=5) %>%   
  group_by(Species) %>% 
  mutate(row=row_number()) %>% 
  pivot_longer(-c(Species, row)) %>%
  mutate(Species = str_to_title(Species),
         name = gsub("\\.", " ", name)) %>% 
  pivot_wider(names_from=c(Species, name), values_from=value)%>% 
  gt() %>% 
  tab_spanner_delim(
    delim="_"
  )
like image 662
Xal Avatar asked Jan 24 '23 07:01

Xal


1 Answers

We could add tab_style with cell_fill and specify the locations with columns and rows

library(dplyr)
library(tidyr)
library(gt) 
library(stringr)

iris %>%   
  group_by(Species) %>%   
  slice_max(Sepal.Length, n=5) %>%   
  group_by(Species) %>% 
  mutate(row=row_number()) %>% 
  pivot_longer(-c(Species, row)) %>%
  mutate(Species = str_to_title(Species),
         name = gsub("\\.", " ", name)) %>% 
  pivot_wider(names_from=c(Species, name), values_from=value)%>% 
  gt() %>%
  tab_spanner_delim(
    delim="_"
  ) %>%
  
  tab_style(
    style = list(
      cell_fill(color = "lightgreen")
      
    ),
    locations = cells_body(
      columns = c(`Setosa_Sepal Length`, `Setosa_Sepal Width`,
  `Versicolor_Sepal Length`, `Versicolor_Sepal Width`, `Versicolor_Petal Length`,
    `Virginica_Sepal Length`,`Virginica_Sepal Width`, `Virginica_Petal Length`),
                
      rows = 1
    )
  )

-ouptut

enter image description here


Update

Based on the comments, if we need to make use of a condition for each column, then we could use a for loop to loop over the column names and add the condition layer and update the original gt object ('tbl1')

tbl1 <- iris %>%   
  group_by(Species) %>%   
  slice_max(Sepal.Length, n=5) %>%   
  group_by(Species) %>% 
  mutate(row=row_number()) %>% 
  pivot_longer(-c(Species, row)) %>%
  mutate(Species = str_to_title(Species),
         name = gsub("\\.", " ", name)) %>% 
  pivot_wider(names_from=c(Species, name), values_from=value)%>% 
  gt() %>%
  tab_spanner_delim(
    delim="_"
  ) 

nm1 <- names(tbl1$`_data`)[-1]

for(i in seq_along(nm1)) {
  
   tbl1 <- tbl1 %>%
     tab_style(
       style = list(
         cell_fill(color = "lightgreen")
         
       ),
       locations = cells_body(
         columns = nm1[i],
         
        rows = seq_along(tbl1$`_data`[[nm1[i]]]) == 1 &  
              tbl1$`_data`[[nm1[i]]] > 3 
       )
     )  
}

-output

enter image description here

like image 193
akrun Avatar answered Feb 03 '23 18:02

akrun