Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrange gt tables side by side or in a grid or table of tables

Tags:

r

r-markdown

gt

I'd like to generate a set of gt table objects in a grid or side-by-side. For example, the code below uses the group_by argument to vertically separate them. But what if I wanted them separated side-by-side?

mtcars2 <-
  mtcars %>% 
  mutate(good_mpg = ifelse(mpg > 20, "Good mileage", "Bad mileage"), 
         car_name = row.names(.)) 

mtcars2 %>% 
  group_by(good_mpg) %>% 
  slice_max(order_by = hp, n=5) %>% 
  arrange(hp) %>% 
  select(car_name, hp) %>% 
  gt() %>% 
  data_color(columns = c("hp"), 
             colors = col_numeric(palette = "Blues", 
                                  domain = c(0, 400)))

table with vertical group_by

like image 987
Daniel Egan Avatar asked Oct 27 '25 06:10

Daniel Egan


2 Answers

@Daniel, thank you for sharing this! This can come in handy.

To make the code a little bit more compact you could use group_map (or do) to generate the two tables within the dplyr workflow, then join them as you did:

library(dplyr)
library(gt)
library(scales)

hp_table <- function(x){
    gt(x) %>% 
        data_color(columns="hp", 
                   colors=col_numeric(palette="Blues", c(0, 400))) %>% 
        tab_options(column_labels.hidden = TRUE) %>% 
        as_raw_html()
}

mtcars %>% 
    mutate(good_mpg = ifelse(mpg > 20, "Good mileage", "Bad mileage"), 
           car_name = row.names(.))  %>% 
    arrange(hp) %>% 
    group_by(relevel(factor(good_mpg), "Good mileage")) %>% 
    slice_head(n=5) %>% 
    select(car_name, hp) %>%
    group_map(~ hp_table(.x)) %>% 
    data.frame(.) %>% 
    setNames(., c("High mileage", "Low mileage")) %>% 
    gt() %>% 
    fmt_markdown(columns = TRUE)

like image 159
user12728748 Avatar answered Oct 28 '25 19:10

user12728748


You can do this by using as_raw_html() for the internal tables, and fmt_markdown(columns = TRUE) in the top-level table.


hp_table <- function(x){
  gt(x) %>% 
    data_color(columns = c("hp"), 
               colors = col_numeric(palette = "Blues", 
                                    domain = c(0, 400))) %>% 
    tab_options(column_labels.hidden = TRUE) %>% 
    as_raw_html() # return as html
}

good_mpg_table <- 
  mtcars %>% 
  mutate(good_mpg = ifelse(mpg > 20, "Good mileage", "Bad mileage"), 
         car_name = row.names(.)) %>% 
  filter(good_mpg == "Good mileage") %>%
  head(5) %>% 
  arrange(hp) %>% 
  select(car_name, hp) %>% 
  hp_table()

bad_mpg_table <- 

  filter(good_mpg == "Bad mileage") %>% 
  head(5) %>% 
  arrange(hp) %>% 
  select(car_name, hp) %>% 
  hp_table() 

data_tables <- data.frame(good_table = good_mpg_table, 
                          bad_table = bad_mpg_table)

data_tables %>% 
  gt() %>% 
  fmt_markdown(columns = TRUE) %>% #render cell contents as html
  cols_label(good_table = "High mileage", 
             bad_table = "Low mileage")

Side-by-side tables

like image 22
Daniel Egan Avatar answered Oct 28 '25 19:10

Daniel Egan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!