rstudio/gt

Using `tab_style` to bold the highest value between two columns in a single row

Open

#1,386 创建于 2023年7月26日

在 GitHub 查看
 (0 评论) (0 反应) (1 负责人)R (2,146 star) (222 fork)batch import
Good First IssuePriority: [2] MediumType: ⁇ Question

描述

Hello,

I am trying to compare two columns and bold the highest value in the first row. The last section of my code is my attempt, however, it bolds both values. How can I fix this?

My other question is about repeating that code 50+ times as I add more rows and columns. What's the best way to loop the tab_style code? Thank you very much for your help!

percentage <- tibble(
  Model = "10-item (Equal, Unordered)",
  N = "1000",
  Class_3_BestAIC = 0,
Class_4_BestAIC = 100,
Class_3_BestCAIC = 0,
Class_4_BestCAIC = 100,
Class_3_BestBIC = 0,
Class_4_BestBIC = 100,
Class_3_BestaBIC = 0,
Class_4_BestaBIC = 100
)

percentage %>%
  gt(rowname_col = "Model") %>%
  tab_header(
    md(
      "*Percentage of Times the Lowest Value Occurred in Each Class Model
for the AIC, CAIC, BIC, and Adjusted BIC*"
    )
  ) %>%
  cols_label(
    N = md("*N*"),
    Class_3_BestAIC = md("*3*"),
    Class_4_BestAIC = md("*4*"),
    Class_3_BestCAIC = md("*3*"),
    Class_4_BestCAIC = md("*4*"),
    Class_3_BestBIC = md("*3*"),
    Class_4_BestBIC = md("*4*"),
    Class_3_BestaBIC = md("*3*"),
    Class_4_BestaBIC = md("*4*")
  ) %>%
  tab_spanner(
    id = "AIC",
    label = md("*Classes*"),
    columns = c(Class_3_BestAIC, Class_4_BestAIC)
  ) %>%
  tab_spanner(
    id = "CAIC",
    label = md("*Classes*"),
    columns = c(Class_3_BestCAIC, Class_4_BestCAIC)
  ) %>%
  tab_spanner(
    id = "BIC",
    label = md("*Classes*"),
    columns = c(Class_3_BestBIC, Class_4_BestBIC)
  ) %>%
  tab_spanner(
    id = "aBIC",
    label = md("*Classes*"),
    columns = c(Class_3_BestaBIC, Class_4_BestaBIC)
  ) %>%
  tab_spanner(
    id = "AIC_Value",
    label = md("*AIC*"),
    spanners = "AIC",
    columns = c(Class_3_BestAIC, Class_4_BestAIC)
  ) %>%
  tab_spanner(
    id = "CAIC_Value",
    label = md("*CAIC*"),
    spanners = "CAIC",
    columns = c(Class_3_BestCAIC, Class_4_BestCAIC)
  ) %>%
  tab_spanner(
    id = "BIC_Value",
    label = md("*BIC*"),
    spanners = "BIC",
    columns = c(Class_3_BestBIC, Class_4_BestBIC)
  ) %>%
  tab_spanner(
    id = "aBIC_Value",
    label = md("*aBIC*"),
    spanners = "aBIC",
    columns = c(Class_3_BestaBIC, Class_4_BestaBIC)
  ) %>%
  tab_spanner(
    label = md("*Latent Class Analysis with Categorical Outcomes*"),
    spanners = c("AIC", "CAIC", "BIC", "aBIC")
  ) %>%
  tab_stubhead(label = md("*Model*")) %>%
  cols_width(1 ~ px(100),
             everything() ~ px(40)) %>%
  cols_align(align = "center") %>%
  cols_align(align = "left",
             columns = 1) %>%
  opt_table_font(font = "Times New Roman") %>%
  opt_align_table_header(align = "left") %>%
  tab_options(
    heading.title.font.size = px(16),
    row_group.as_column = TRUE,
    stub.border.width = 0,
    heading.border.bottom.color = "black",
    column_labels.border.top.color = "black",
    column_labels.border.bottom.color = "black",
    table_body.border.bottom.color = "black",
    table.border.bottom.color = "black",
    table.border.top.color = "black",
    table.background.color = "white"
  ) %>%
  tab_style(
    style = list(
      cell_borders(
        sides = c("top", "bottom"),
        color = "white",
        weight = px(1)
      ),
      cell_text(align = "center"),
      cell_fill(color = "white", alpha = NULL)
    ),
    locations = cells_body(columns = everything(),
                           rows = everything())
  ) %>%
  tab_style(
    style = list(
      cell_text(weight = "bold")
            ),
    locations = list(
    cells_body(
     columns = c(Class_3_BestAIC,Class_4_BestAIC),
     row = 1)
  )
) 

贡献者指南

Using `tab_style` to bold the highest value between two columns in a single row · rstudio/gt#1386 | Good First Issue