Formatting gt table

Hello. I am trying to make a table in gt with this data:

ab <- data.frame(
  stringsAsFactors = FALSE,
           Country = c("Germany", "Australia", "Ireland", "France", "Denmark"),
        Operations = c(3L, 1L, 2L, 1L, 1L),
             Euros = c(79545479, 3530267, 56855826, 7759, 1230071),
              Tons = c(1230497, 48, 57704, 228, 249)

I use this code:

ab %>% 
  gt() %>% 
  fmt_number(columns = 3:4, decimals = 2, sep_mark = ".", dec_mark = ",") %>% 
  gt_highlight_rows(rows = c(1,2), font_weight = "bold") %>% 
  gt_highlight_rows(rows = c(3,4), font_weight = "bold", fill = "lightblue") %>%
  grand_summary_rows(columns = c(Operations, Euros, Tons),
                     fns = list(Total = "sum")) %>% 
  gt_theme_nytimes()

I obtain this:

How can I move the total to below the Country column and remove the extra column in the left? And format the text in the same style as the rest of the table?

Thanks all.

if you don't like how grand_summary_rows works you can summarise it manually.

ab <- data.frame(
  stringsAsFactors = FALSE,
  Country = c("Germany", "Australia", "Ireland", "France", "Denmark"),
  Operations = c(3L, 1L, 2L, 1L, 1L),
  Euros = c(79545479, 3530267, 56855826, 7759, 1230071),
  Tons = c(1230497, 48, 57704, 228, 249))

library(gt)
library(gtExtras) # renv::install("jthomasmock/gtExtras")
library(tidyverse)
(absummary <- summarise(ab,across(where(is.numeric),sum)) %>% mutate(Country="Total") %>% relocate(Country))

bind_rows(ab,absummary) %>%
  gt() %>% 
  fmt_number(columns = 3:4, decimals = 2, sep_mark = ".", dec_mark = ",") %>% 
  gt_highlight_rows(rows = c(1,2)) %>% 
  gt_highlight_rows(rows = c(3,4), fill = "lightblue") %>%
  # grand_summary_rows(columns = c(Operations, Euros, Tons),
  #                    fns = list(Total = "sum")) %>% 
  gt_theme_nytimes()
1 Like

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.