{gtExtras} 列在分组时在 {gt} table 中以错误的顺序显示

{gtExtras} column showing in wrong order in {gt} table when grouped

我正在制作 gt table 来显示个人在实现目标方面取得的进展。在 table 中,有一行显示了该目标的进度水平条形图(如果目标为 50 且分数为 40,则条形图为 80%)。

但是,当我使用 groupname_col 参数更改 gt 行的顺序时,其他单元格的顺序会更改,但 gtExtras gt_plt_bar_pct 的顺序不会更改列,所以它在该行中显示了错误的名称和分数条,相反,该列似乎总是按照输入数据中的行顺序表示。

我知道我可以在 gt 开始之前在 df 上使用 arrange 来解决这个问题,但这似乎不是一个好的解决方案,因为我想要改变不同组查看的行的顺序。这是 gtExtras 的缺陷吗?有更好的解决方法吗?

谢谢!

代表:

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

这是上述代码的输出:请注意,条形图的长度并不总是反映它们所在行的值。相反,它们反映了原始数据集的顺序。

编辑:删除 row_group_order。如果我再次 运行 上面的代码,但注释掉意味着重新排列组外观的行,则分组以不同的顺序显示(组在原始数据集中的出现顺序),并且名称和第一个两列相应地排序到这些组中,但条形图列仍然没有,并保持数据集的原始顺序。下图:

根据 gtExtras v 0.2.4 此错误已得到修复。感谢您的养育和伟大的reprex

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