Skip to content

tab_style() location argument does not match location of behavior if input data is a grouped data frame #2133

Open

Description

@ashleymullan

Description

When creating a gt() from a grouped data frame and calling tab_style() to color-code body cells with conditional logic, the location of the color-code is incorrect. I am using R version 4.4.1, dplyr version 1.2.0, and gt version 1.0.0. Thanks for any advice!

Reproducible example

Note that the "color_code" column spits out the correct color code to aid in finding mismatches.

library(dplyr) 
library(gt) 



create_dummy_data <- function(seed){
    set.seed(seed)
    dummy_data <- data.frame(
                             person = sample(c("Jane", "Lily", "Kathy",
                                                        "Dave", "Cliff", "Harry",
                                                        "Trey", "Lana", "Rosie",
                                                        "Eric", "Scott"),
                                                      size = 50,
                                                      replace = TRUE),
                             total_flags = sample(0:5, 50, TRUE))

    dummy_data 
}

create_table <- function(dummy_data){
    dummy_data |>
      mutate(color_code = case_when(
        total_flags <= 1 ~ "🟢",
        total_flags > 1 & total_flags < 4 ~ "🟡",
        total_flags >= 4 ~ "🔴"
      )) |>
      group_by(person) |>
      gt(groupname_col = "person") |>
      cols_label(total_flags = "Total Flags",
                 color_code = "Case Code") |>
      tab_options(ihtml.active = FALSE, container.height = pct(100),
                  row_group.as_column = TRUE,
                  container.overflow.x = TRUE,
                  container.overflow.y = TRUE) |>
      opt_interactive(use_page_size_select = TRUE,  use_sorting = TRUE,
                      use_filters = TRUE,
                      use_search = TRUE, use_resizers = TRUE,
                      use_highlight = TRUE)  |>

    tab_style(
      style = cell_fill(color = "#4cbb17"),
      locations = cells_body(
        columns = total_flags,
        rows = total_flags <= 1)) |>
    tab_style(
      style = cell_fill(color = "#e3c565"),
      locations = cells_body(
        columns = total_flags,
        rows = total_flags > 1 & total_flags < 4)) |>
      tab_style(
        style = cell_fill(color = "red"),
        locations = cells_body(
          columns = total_flags,
          rows = total_flags >= 4))
  }


dummy_data3 <- create_dummy_data(1031)
create_table(dummy_data3)

sessionInfo()

Expected result

The cells in total_flags should be colored according to the emoji next to them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions