1

Hi guys how are you? I am trying to make a nice table using gt package in R. So far i managed very well, its a nice package, very intuitive. But im struggling trying to center the label on the stubhead section. I tried a lot of thins with no success. Do you know how can i achieve this?. I created a piece of code with fake data to understand the problem better. I would like to center the label called "INDICADOR".

df=data.frame(indicador= c("MAP","MAC","MB", "REP","APU"),
              actual= c(100,100,100,200,200),
              anual=c(1,2,3,4,5),
              rep=c(0.78,0.21,0.54,0.33,0.7), 
              var=c(1.2,1.2,1.5,1.3,1.1))


df %>% mutate(NewRowNames=c("MONTO APUESTA","MONTO ACIERTO",
                            "MARGEN BRUTO","REPAGO","APUESTAS")) %>%
  column_to_rownames(var="NewRowNames") %>% select(-indicador) %>% 
  select(-rep) %>%
  gt(rownames_to_stub = T) %>% 
  tab_stubhead(label="INDICADOR") %>% 
  tab_header(title = md("**KPI´s REPORTE SEMANAL**")) %>% 
  fmt_currency(rows = c(1,2,3),
               columns= c(2,3),
               dec_mark = ",",
               sep_mark = ".",
               decimals = 0) %>% 
  fmt_percent(columns = c("var"),
              scale_values = T) %>% 
  fmt_number(rows = 5,
             columns= c(2,3),
             sep_mark = ".",
             dec_mark = ",",
             decimals = 0) %>% 
  fmt_percent(columns = c(2,3),
              rows=4,
              scale_values = T) %>% 
  cols_align(align = "center",
             columns = everything()) %>%
  gt_theme_espn() %>% 
  opt_align_table_header(align="center")
Emiliano Barone
  • 303
  • 2
  • 11
  • It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a working code example which others could run and a snippet of your data or some fake data. – stefan May 24 '23 at 19:47
  • Thanks for the advice @stefan , i already did an edit to clarify the question. Hope it helps. – Emiliano Barone May 24 '23 at 20:02

1 Answers1

1

One option to center the stub label would be to use tab_style like so:

tab_style(
  style = cell_text(align = "center"),
  locations = cells_stubhead()
)

Complete example code:

library(gt)
library(dplyr)
library(espnscrapeR)

df %>%
  mutate(NewRowNames = c(
    "MONTO APUESTA", "MONTO ACIERTO",
    "MARGEN BRUTO", "REPAGO", "APUESTAS"
  )) %>%
  column_to_rownames(var = "NewRowNames") %>%
  select(-indicador) %>%
  select(-rep) %>%
  gt(rownames_to_stub = T) %>%
  tab_stubhead(label = "INDICADOR") %>%
  tab_header(title = md("**KPI´s REPORTE SEMANAL**")) %>%
  fmt_currency(
    rows = c(1, 2, 3),
    columns = c(2, 3),
    dec_mark = ",",
    sep_mark = ".",
    decimals = 0
  ) %>%
  fmt_percent(
    columns = c("var"),
    scale_values = T
  ) %>%
  fmt_number(
    rows = 5,
    columns = c(2, 3),
    sep_mark = ".",
    dec_mark = ",",
    decimals = 0
  ) %>%
  fmt_percent(
    columns = c(2, 3),
    rows = 4,
    scale_values = T
  ) %>%
  cols_align(
    align = "center",
    columns = everything()
  ) %>%
  gt_theme_espn() %>%
  opt_align_table_header(align = "center") |> 
  tab_style(
    style = cell_text(align = "center"),
    locations = cells_stubhead()
  )

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51