jthomasmock / gtExtras

A Collection of Helper Functions for the gt Package.
https://jthomasmock.github.io/gtExtras/
Other
195 stars 27 forks source link

gtExtras rows don't change position when gt rows do, using groups #38

Closed jakelawlor closed 2 years ago

jakelawlor commented 2 years ago

Hello,

I'm not totally sure if this is a bug or if it's user error, but I am noticing that columns in my table that are made with {gtExtras} are out of order when I rearrange my {gt} table by using groups.

I have a stackoverflow post about this here: https://stackoverflow.com/questions/71313688/gtextras-column-showing-in-wrong-order-in-gt-table-when-grouped but I will repeat the reprex below.


I am making a gt table showing the progress of individuals towards their goals. In the table, there is a row showing a horizontal bar graph of progress towards that goal (if goal is 50 and score is 40, the bar is at 80%).

However, when I change the order of the gt rows by using the groupname_col argument, the order of the other columns changes, as well as the order of the tab_style formatting, but the order of the gtExtras::gt_plt_bar_pct() column does not. It appears the gtExtras::gt_plt_bar_pct always appears in the same order of input data, rather than the order that is set inside gt() function. This means that when I rearrange my table by group, the bar chart column ends up showing the wrong bar to go with the score and name in that row.

I can fix this by using arrange() on the input data before the gt() begins, but this doesn't seem like a permanent solution since I'm going to want to change the order of the rows to view by different groups. Like I said, I am not positive this is a bug, but I expected the columns made with gtExtras would behave in the same way as columns made in gt.

Thanks for your advice, and I love the package overall!

library(tibble)
library(gt)
library(gtExtras)
library(dplyr) 

# make dataframe of individuals and their goals
df <- tribble(
  ~name, ~group, ~score, ~goal,
    "Bob", "C",   20,   40,
    "Chris",  "A", 50,   40,
    "Dale",  "B",  30,   50,
    "Jay",    "A", 0,   40,
     "Ben",   "B", 10,   20 ) %>%
  # calculate percent towards goal, and cap at 100%
  mutate(percent_to_goal = score/goal *100,
         percent_to_goal = case_when(percent_to_goal >= 100 ~ 100,
                                     TRUE ~ percent_to_goal))

df %>%

  # this fixes the issue, but doesn't seem like a permanent solution
  #arrange(group, name) %>%

  # make gt table
  gt(rowname_col = "name", groupname_col = "group") %>%

  # order groups
  row_group_order(groups = c("A","B","C")) %>%

  # add bar chart column
  gt_plt_bar_pct(column = percent_to_goal)  %>%

  # highlight blue if person reaches their goal
  tab_style(
    style = list(
      cell_fill(color = "lightcyan"),
      cell_text(weight = "bold")),
    locations = cells_body(
      columns = c(goal,score, percent_to_goal),
      rows = score >= goal
    )
  ) 

In the image, the first row is Chris, who met their goal (so the row is blue), but the bar chart is showing the score of Bob (50% towards goal), the first row in the input dataframe.

image

jthomasmock commented 2 years ago

Fixed in v 0.2.4 - thanks for raising!

library(tibble)
library(gt)
library(gtExtras)
library(dplyr) 

# make dataframe of individuals and their goals
df <- tribble(
  ~name, ~group, ~score, ~goal,
  "Bob", "C",   20,   40,
  "Chris",  "A", 50,   40,
  "Dale",  "B",  30,   50,
  "Jay",    "A", 0,   40,
  "Ben",   "B", 10,   20

) %>%
  # calculate percent towards goal, and cap at 100%
  mutate(percent_to_goal = score/goal *100,
    percent_to_goal = case_when(percent_to_goal >= 100 ~ 100,
      TRUE ~ percent_to_goal))

df %>%
  # make gt table
  gt(rowname_col = "name", groupname_col = "group") %>%

  # order groups
  row_group_order(groups = c("A","B","C")) %>%

  # add bar chart column
  gt_plt_bar_pct(column = percent_to_goal)  %>%

  # highlight blue if person reaches their goal
  tab_style(
    style = list(
      cell_fill(color = "lightcyan"),
      cell_text(weight = "bold")),
    locations = cells_body(
      columns = c(goal,score, percent_to_goal),
      rows = score >= goal
    )
  ) 

Table with inline barcharts