在R中将stubhead部分的标签居中对齐。

huangapple go评论61阅读模式
英文:

Centering label of stubhead section in gt table R

问题

你好,我理解你想要翻译的部分。以下是翻译好的内容:

"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")

"

英文:

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")

答案1

得分: 1

以下是翻译好的部分:

要将标签居中对齐的一个选项是使用 tab_style,如下所示:

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

完整示例代码:

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 = TRUE) %>%
  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 = TRUE
  ) %>%
  fmt_number(
    rows = 5,
    columns = c(2, 3),
    sep_mark = ".",
    dec_mark = ",",
    decimals = 0
  ) %>%
  fmt_percent(
    columns = c(2, 3),
    rows = 4,
    scale_values = TRUE
  ) %>%
  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()
  )

在R中将stubhead部分的标签居中对齐。

英文:

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()
  )

在R中将stubhead部分的标签居中对齐。

huangapple
  • 本文由 发表于 2023年5月25日 03:34:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定