Skip to content

Commit 982fa30

Browse files
authored
Merge pull request #2112 from thebioengineer/latex_columnwidth_handle_rowgroupcolumn
handle rowgroups as columns and add tests.
2 parents ae4045c + 2846b56 commit 982fa30

File tree

3 files changed

+169
-7
lines changed

3 files changed

+169
-7
lines changed

NEWS.md

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

55
* Added `stub.separate` option to control if vertical bars/borders are added for stubs (#2096). (@thebioengineer)
66

7+
* Fixed handling of row_groups_as_column=TRUE for latex columns (#2110). (@thebioengineer)
8+
79
# gt 1.2.0
810

911
* The new `fmt_number_si()` function format numeric values with SI prefixes and an optional unit (which could be also be obtained from a separate column), automatically selecting the appropriate prefix to keep the mantissa in a readable range (#1999). (#2060)

R/utils_render_latex.R

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,10 +1606,21 @@ create_body_rows_l <- function(
16061606
grepl("^::stub_.*::$", colname_i)
16071607
) {
16081608

1609+
i_offset <- 0
1610+
if ("::group::" %in% vars) {
1611+
i_offset <- 1
1612+
}
1613+
16091614
colwidth_i <- dplyr::filter(
16101615
colwidth_df,
16111616
type == "stub",
1612-
)[i, ]
1617+
)[i - i_offset, ]
1618+
1619+
} else if (identical(colname_i, "::group::")) {
1620+
colwidth_i <- dplyr::filter(
1621+
colwidth_df,
1622+
type == "row_group"
1623+
)
16131624

16141625
} else {
16151626
colwidth_i <- dplyr::filter(
@@ -1618,10 +1629,11 @@ create_body_rows_l <- function(
16181629
)
16191630
}
16201631

1621-
if (sum(colwidth_i$unspec < 1) > 0) {
1622-
cell_width <- create_singlecolumn_width_text_l(pt = colwidth_i$pt, lw = colwidth_i$lw)
1623-
} else {
1624-
cell_width <- ""
1632+
cell_width <- ""
1633+
if (nrow(colwidth_i) > 0) {
1634+
if (sum(colwidth_i$unspec < 1) > 0) {
1635+
cell_width <- create_singlecolumn_width_text_l(pt = colwidth_i$pt, lw = colwidth_i$lw)
1636+
}
16251637
}
16261638

16271639
content[i] <-
@@ -1638,10 +1650,21 @@ create_body_rows_l <- function(
16381650
identical(colname_i, "::stub::") ||
16391651
grepl("^::stub_.*::$", colname_i)
16401652
) {
1653+
i_offset <- 0
1654+
if ("::group::" %in% vars) {
1655+
i_offset <- 1
1656+
}
1657+
16411658
colwidth_i <- dplyr::filter(
16421659
colwidth_df,
16431660
type == "stub",
1644-
)[i, ]
1661+
)[i - i_offset, ]
1662+
1663+
} else if (identical(colname_i, "::group::")) {
1664+
colwidth_i <- dplyr::filter(
1665+
colwidth_df,
1666+
type == "row_group"
1667+
)
16451668

16461669
} else {
16471670
colwidth_i <- dplyr::filter(
@@ -1701,7 +1724,7 @@ remove_footnote_encoding <- function(x) {
17011724
} else if (grepl("%%%left:",x_i)) {
17021725
footmark_text <- regmatches(x_i, regexec("(?<=%%%left:).+?$", x_i, perl = TRUE))[[1]]
17031726
content_x <- regmatches(x_i, regexec(".+?(?=%%%left:)", x_i, perl = TRUE))[[1]]
1704-
1727+
17051728
# Add footmark within shortstack
17061729
if (grepl("\\\\shortstack", content_x)) {
17071730
x_i <- gsub("(\\\\shortstack\\[.\\]\\{(\\\\parbox\\{.+?\\}\\{)*)(.+?\\})", paste0("\\1", gsub("\\", "\\\\", footmark_text, fixed=TRUE), " \\3"), content_x, perl = TRUE)

tests/testthat/test-l_cols_width.R

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,140 @@ test_that("cols_width() works correctly in LaTeX output tables", {
213213

214214
expect_match(tbl_latex_tabul, "\\\\begin\\{tabular\\*\\}\\{\\\\linewidth\\}\\{@\\{\\\\extracolsep\\{\\\\fill\\}\\}>\\{\\\\raggedright\\\\arraybackslash\\}p\\{\\\\dimexpr 37.50pt -2\\\\tabcolsep-1.5\\\\arrayrulewidth\\}|>\\{\\\\raggedright\\\\arraybackslash\\}p\\{\\\\dimexpr 56.25pt -2\\\\tabcolsep-1.5\\\\arrayrulewidth\\}|>\\{\\\\raggedleft\\\\arraybackslash\\}p\\{\\\\dimexpr 112.50pt -2\\\\tabcolsep-1.5\\\\arrayrulewidth\\}>\\{\\\\raggedleft\\\\arraybackslash\\}p\\{\\\\dimexpr 150.00pt -2\\\\tabcolsep-1.5\\\\arrayrulewidth\\}\\}")
215215
})
216+
217+
218+
test_that("cols_width() works correctly in LaTeX output tables when row_group_as_column is TRUE or FALSE", {
219+
220+
tbl <-
221+
dplyr::tibble(
222+
row = 1:6,
223+
group = c(rep("Group A<br>test", 3), rep("Group B", 3)),
224+
vals = 1:6
225+
)
226+
227+
tbl_rgac <- tbl |>
228+
gt(
229+
rowname_col = "row",
230+
groupname_col = "group",
231+
row_group_as_column = TRUE,
232+
process_md = TRUE
233+
) |>
234+
fmt_markdown() |>
235+
cols_width(
236+
row_group() ~ px(200),
237+
stub() ~ px(100),
238+
vals ~ px(50)
239+
) |>
240+
tab_style(style = list(cell_text(align = "right")), cells_stub()) |>
241+
as_latex() |>
242+
as.character()
243+
244+
expect_length(tbl_rgac, 1)
245+
246+
## rowgroup column
247+
expect_match(tbl_rgac, "\\\\multirow\\[t\\]\\{3\\}\\{=\\}\\{\\\\shortstack\\[l\\]\\{\\\\parbox\\{\\\\linewidth\\}\\{Group A \\\\\\\\test\\}\\}\\}")
248+
expect_match(tbl_rgac, "\\\\multirow\\[t\\]\\{3\\}\\{=\\}\\{Group B\\}")
249+
250+
## styling stub
251+
expect_match(tbl_rgac, "\\multicolumn{1}{>{\\raggedleft\\arraybackslash}m{\\dimexpr 75.00pt -2\\tabcolsep-1.5\\arrayrulewidth}}{\\parbox{\\linewidth}{\\raggedleft {1}}}", fixed = TRUE)
252+
253+
254+
255+
tbl_rgnac <- tbl |>
256+
gt(
257+
rowname_col = "row",
258+
groupname_col = "group",
259+
row_group_as_column = FALSE,
260+
process_md = TRUE
261+
) |>
262+
fmt_roman(columns = stub()) |>
263+
fmt_markdown() |>
264+
cols_width(
265+
row_group() ~ px(200),
266+
stub() ~ px(100),
267+
vals ~ px(50)
268+
) |>
269+
tab_style(style = list(cell_text(align = "right")), cells_stub()) |>
270+
as_latex() |>
271+
as.character()
272+
273+
expect_length(tbl_rgnac, 1)
274+
275+
## rowgroup column
276+
expect_match(tbl_rgnac, "\\\\multicolumn\\{2\\}\\{>\\{\\\\raggedright\\\\arraybackslash\\}m\\{262.5pt\\}\\}\\{\\\\shortstack\\[l\\]\\{\\\\parbox\\{\\\\linewidth\\}\\{Group A \\\\\\\\test\\}\\}\\}")
277+
expect_match(tbl_rgnac, "\\\\multicolumn\\{2\\}\\{>\\{\\\\raggedright\\\\arraybackslash\\}m\\{262.5pt\\}\\}\\{\\\\parbox\\{\\\\linewidth\\}\\{Group B\\}\\}")
278+
279+
## styling stub
280+
expect_match(tbl_rgnac, "\\multicolumn{1}{>{\\raggedleft\\arraybackslash}m{\\dimexpr 75.00pt -2\\tabcolsep-1.5\\arrayrulewidth}}{\\parbox{\\linewidth}{\\raggedleft {1}}}", fixed = TRUE)
281+
282+
})
283+
284+
285+
test_that("cols_width() works correctly in LaTeX output tables when row_group_as_column is TRUE or FALSE, multistub", {
286+
287+
tbl <-
288+
dplyr::tibble(
289+
row1 = 1:6,
290+
row2 = LETTERS[1:6],
291+
group = c(rep("Group A<br>test", 3), rep("Group B", 3)),
292+
vals = 1:6
293+
)
294+
295+
tbl_rgac <- tbl |>
296+
gt(
297+
rowname_col = c("row1","row2"),
298+
groupname_col = "group",
299+
row_group_as_column = TRUE,
300+
process_md = TRUE
301+
) |>
302+
fmt_markdown() |>
303+
cols_width(
304+
row_group() ~ px(200),
305+
stub() ~ px(100),
306+
vals ~ px(50)
307+
) |>
308+
tab_style(style = list(cell_text(align = "right")), cells_stub()) |>
309+
as_latex() |>
310+
as.character()
311+
312+
expect_length(tbl_rgac, 1)
313+
314+
## rowgroup column
315+
expect_match(tbl_rgac, "\\\\multirow\\[t\\]\\{3\\}\\{=\\}\\{\\\\shortstack\\[l\\]\\{\\\\parbox\\{\\\\linewidth\\}\\{Group A \\\\\\\\test\\}\\}\\}")
316+
expect_match(tbl_rgac, "\\\\multirow\\[t\\]\\{3\\}\\{=\\}\\{Group B\\}")
317+
318+
## styling stub
319+
expect_match(tbl_rgac, "\\multicolumn{1}{>{\\raggedleft\\arraybackslash}m{\\dimexpr 75.00pt -2\\tabcolsep-1.5\\arrayrulewidth}}{\\parbox{\\linewidth}{\\raggedleft {1}}}", fixed = TRUE)
320+
expect_match(tbl_rgac, "\\multicolumn{1}{>{\\raggedleft\\arraybackslash}m{\\dimexpr 75.00pt -2\\tabcolsep-1.5\\arrayrulewidth}}{\\parbox{\\linewidth}{\\raggedleft {A}}}", fixed = TRUE)
321+
322+
323+
324+
tbl_rgnac <- tbl |>
325+
gt(
326+
rowname_col = c("row1","row2"),
327+
groupname_col = "group",
328+
row_group_as_column = FALSE,
329+
process_md = TRUE
330+
) |>
331+
fmt_roman(columns = stub()) |>
332+
fmt_markdown() |>
333+
cols_width(
334+
row_group() ~ px(200),
335+
stub() ~ px(100),
336+
vals ~ px(50)
337+
) |>
338+
tab_style(style = list(cell_text(align = "right")), cells_stub()) |>
339+
as_latex() |>
340+
as.character()
341+
342+
expect_length(tbl_rgnac, 1)
343+
344+
## rowgroup column
345+
expect_match(tbl_rgnac, "\\\\multicolumn\\{3\\}\\{>\\{\\\\raggedright\\\\arraybackslash\\}m\\{337.5pt\\}\\}\\{\\\\shortstack\\[l\\]\\{\\\\parbox\\{\\\\linewidth\\}\\{Group A \\\\\\\\test\\}\\}\\}")
346+
expect_match(tbl_rgnac, "\\\\multicolumn\\{3\\}\\{>\\{\\\\raggedright\\\\arraybackslash\\}m\\{337.5pt\\}\\}\\{\\\\parbox\\{\\\\linewidth\\}\\{Group B\\}\\}")
347+
348+
## styling stub
349+
expect_match(tbl_rgnac, "\\multicolumn{1}{>{\\raggedleft\\arraybackslash}m{\\dimexpr 75.00pt -2\\tabcolsep-1.5\\arrayrulewidth}}{\\parbox{\\linewidth}{\\raggedleft {1}}}", fixed = TRUE)
350+
expect_match(tbl_rgnac, "\\multicolumn{1}{>{\\raggedleft\\arraybackslash}m{\\dimexpr 75.00pt -2\\tabcolsep-1.5\\arrayrulewidth}}{\\parbox{\\linewidth}{\\raggedleft {A}}}", fixed = TRUE)
351+
352+
})

0 commit comments

Comments
 (0)