Skip to content

Commit a982f55

Browse files
authored
Merge pull request #1801 from olivroy/incorrect-stub
`row_group_as_column = TRUE` + 2 group variables
2 parents 92b08d9 + 52f3d81 commit a982f55

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
* Improved width calculations in `as_gtable()` (@teunbrand, #1923)
1818

19+
* `gt(row_group_as_column = TRUE)` now works if multiple groups are supplied (@olivroy, #1552).
20+
1921
# gt 0.11.1
2022

2123
## Breaking changes

R/dt_boxhead.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ dt_boxhead_get_alignments_in_stub <- function(data) {
264264
var = grp_vars
265265
)
266266

267+
if (length(grp_vars) > 1) {
268+
grp_alignment <- grp_alignment[1]
269+
}
270+
267271
alignments <- c(alignments, grp_alignment)
268272
}
269273

@@ -294,6 +298,13 @@ dt_boxhead_get_vars_align_default <- function(data) {
294298

295299
dt_boxhead_get_alignment_by_var <- function(data, var) {
296300
boxhead <- dt_boxhead_get(data = data)
301+
if (length(var) > 1L) {
302+
# return multiple alignments in case of multiple variables
303+
# requested. #1552
304+
return(
305+
boxhead$column_align[boxhead$var %in% var]
306+
)
307+
}
297308
boxhead$column_align[boxhead$var == var]
298309
}
299310

R/dt_groups_rows.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ dt_groups_rows_build <- function(data, context) {
105105

106106
groups_rows[is.na(groups_rows[, "group_id"]), "group_label"] <-
107107
others_group
108+
if (!is.null(stub_df$group_label)) {
109+
# stub_df$group_label[is.null(stub_df$group_label[[1]])] <- others_group
110+
#data <- dt_stub_df_set(data, stub_df)
111+
112+
}
108113

109114
} else {
110115

@@ -132,5 +137,6 @@ dt_groups_rows_build <- function(data, context) {
132137
}
133138
}
134139

140+
# print(groups_rows)
135141
dt_groups_rows_set(data = data, groups_rows = groups_rows)
136142
}

tests/testthat/test-stub.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,25 @@ test_that("The stub can be formatted with `fmt_*()` functions and `stub()", {
429429
selection_text(selection = "[class='gt_footnote']") %>%
430430
expect_equal(c("1 footnote `3`", "2 footnote `4`"))
431431
})
432+
433+
test_that("gt(row_group_as_column = TRUE) works with multiple `groupname_col` (#1552)", {
434+
many_grps <- gt(
435+
utils::head(gtcars, n = 8),
436+
groupname_col = c("ctry_origin", "mfr"),
437+
row_group_as_column = TRUE
438+
)
439+
stub_look <- xml2::read_xml(render_as_html(many_grps))
440+
row_groups <- stub_look %>%
441+
selection_text(
442+
"[class='gt_row_group_first']"
443+
)
444+
# we expect 2 groups here
445+
expect_match(
446+
row_groups[1],
447+
"United States.+Ford"
448+
)
449+
expect_match(
450+
row_groups[2],
451+
"Italy.+Ferrari"
452+
)
453+
})

tests/testthat/test-utils_render_grid.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,16 @@ test_that("body_cells_g() creates appropriate cells", {
170170

171171
expect_equal(vctrs::list_sizes(test), c(3, 3, 3))
172172
test <- vctrs::vec_c(!!!test)
173+
# FIXME with row.striping.include_stub = TRUE,
173174
expect_equal(
174175
has_class(test, "gt_striped"),
175176
rep(c(FALSE, TRUE, FALSE), each = 3)
176177
)
178+
# Has the correct class, but is not striped?
179+
expect_contains(
180+
unlist(test[test$label == "B", ]$classes),
181+
c("gt_stub", "gt_striped")
182+
)
177183
expect_equal(
178184
has_class(test, "gt_stub"),
179185
rep(c(TRUE, FALSE, FALSE), 3)

0 commit comments

Comments
 (0)