Skip to content

Commit

Permalink
Merge pull request #1732 from olivroy/summary-rows
Browse files Browse the repository at this point in the history
Refactor tests and `summary_rows()`
  • Loading branch information
rich-iannone authored Jun 26, 2024
2 parents c545dfe + 2a30f45 commit a46ff48
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 410 deletions.
36 changes: 10 additions & 26 deletions R/dt_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ dt_summary_build <- function(data, context) {
.data[[group_id_col_private]]
)

# TODO find a way to switch to across()
# https://github.com/tidyverse/dplyr/issues/6707
select_data_tbl <-
dplyr::ungroup(
dplyr::summarize_at(
Expand All @@ -238,14 +240,8 @@ dt_summary_build <- function(data, context) {
)
)

select_data_tbl <-
dplyr::mutate_all(
select_data_tbl,
.funs = function(x) {
x[is.nan(x)] <- NA
x
}
)
# Replace NaN by NA
select_data_tbl[is.na(select_data_tbl)] <- NA

select_data_tbl <-
dplyr::mutate(
Expand Down Expand Up @@ -297,14 +293,9 @@ dt_summary_build <- function(data, context) {
locale = resolve_locale(data = data, locale = NULL)
)

summary_dfs_display_gt[["_data"]] <-
dplyr::mutate_all(
summary_dfs_display_gt[["_data"]],
.funs = function(x) {
x[is.nan(x)] <- NA
x
}
)
# Replace NaN with NA
summary_dfs_display_gt[["_data"]][is.na(summary_dfs_display_gt[["_data"]])] <-
NA

summary_dfs_display_gt[["_stub_df"]] <-
dplyr::mutate(
Expand All @@ -321,7 +312,7 @@ dt_summary_build <- function(data, context) {
# Determine if we are actually formatting a grand summary section;
# in that case we'd want to ignore any supplied group directive
group_is_grand_summary <-
length(groups) == 1 && groups == "::GRAND_SUMMARY"
identical(groups, "::GRAND_SUMMARY")

if (!is.null(format_lhs) && !group_is_grand_summary) {

Expand All @@ -348,7 +339,7 @@ dt_summary_build <- function(data, context) {
# formatted to the group

fmt_expr_lines <- deparse(rlang::f_rhs(fmt_exprs[[k]]))
fmt_expr_lines <- gsub("^\\s+", "", fmt_expr_lines)
fmt_expr_lines <- trimws(fmt_expr_lines, "left", " ")
format_fn_grp <- paste(fmt_expr_lines, collapse = "")

fmt_expr_names <- names(rlang::f_rhs(fmt_exprs[[k]]))
Expand Down Expand Up @@ -416,14 +407,7 @@ dt_summary_build <- function(data, context) {
summary_dfs_display[["::group_id::"]] <- summary_dfs_data[["::group_id::"]]
summary_dfs_display[["::row_id::"]] <- summary_dfs_data[["::row_id::"]]

summary_dfs_display <-
dplyr::mutate_all(
summary_dfs_display,
.funs = function(x) {
x[x == "NA"] <- NA
x
}
)
summary_dfs_display[summary_dfs_display == "NA"] <- NA

summary_dfs_display$`::rowname::` <- NA_character_

Expand Down
40 changes: 16 additions & 24 deletions man/extract_summary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/cols_merge.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
Output
[1] "<table class=\"gt_table\" data-quarto-disable-processing=\"false\" data-quarto-bootstrap=\"false\">\n <thead>\n <tr class=\"gt_col_headings\">\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"mpg\">mpg</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"cyl\">cyl</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"disp\">disp</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"hp\">hp</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"drat\">drat</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"qsec\">qsec</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"vs\">vs</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"am\">am</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"gear\">gear</th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"carb\">carb</th>\n </tr>\n </thead>\n <tbody class=\"gt_table_body\">\n <tr><td headers=\"mpg\" class=\"gt_row gt_right\">21.0</td>\n<td headers=\"cyl\" class=\"gt_row gt_right\">6</td>\n<td headers=\"disp\" class=\"gt_row gt_right\">160</td>\n<td headers=\"hp\" class=\"gt_row gt_right\">110</td>\n<td headers=\"drat\" class=\"gt_row gt_right\">3.90</td>\n<td headers=\"qsec\" class=\"gt_row gt_right\">16.46</td>\n<td headers=\"vs\" class=\"gt_row gt_right\">0</td>\n<td headers=\"am\" class=\"gt_row gt_right\">1</td>\n<td headers=\"gear\" class=\"gt_row gt_right\">4</td>\n<td headers=\"carb\" class=\"gt_row gt_right\">4</td></tr>\n <tr><td headers=\"mpg\" class=\"gt_row gt_right\">21.0</td>\n<td headers=\"cyl\" class=\"gt_row gt_right\">6</td>\n<td headers=\"disp\" class=\"gt_row gt_right\">160</td>\n<td headers=\"hp\" class=\"gt_row gt_right\">110</td>\n<td headers=\"drat\" class=\"gt_row gt_right\">3.90 (2.875)</td>\n<td headers=\"qsec\" class=\"gt_row gt_right\">17.02</td>\n<td headers=\"vs\" class=\"gt_row gt_right\">0</td>\n<td headers=\"am\" class=\"gt_row gt_right\">1</td>\n<td headers=\"gear\" class=\"gt_row gt_right\">4</td>\n<td headers=\"carb\" class=\"gt_row gt_right\">4</td></tr>\n <tr><td headers=\"mpg\" class=\"gt_row gt_right\">22.8</td>\n<td headers=\"cyl\" class=\"gt_row gt_right\">4</td>\n<td headers=\"disp\" class=\"gt_row gt_right\">108</td>\n<td headers=\"hp\" class=\"gt_row gt_right\">93</td>\n<td headers=\"drat\" class=\"gt_row gt_right\">3.85</td>\n<td headers=\"qsec\" class=\"gt_row gt_right\">18.61</td>\n<td headers=\"vs\" class=\"gt_row gt_right\">1</td>\n<td headers=\"am\" class=\"gt_row gt_right\">1</td>\n<td headers=\"gear\" class=\"gt_row gt_right\">4</td>\n<td headers=\"carb\" class=\"gt_row gt_right\">1</td></tr>\n <tr><td headers=\"mpg\" class=\"gt_row gt_right\">21.4</td>\n<td headers=\"cyl\" class=\"gt_row gt_right\">6</td>\n<td headers=\"disp\" class=\"gt_row gt_right\">258</td>\n<td headers=\"hp\" class=\"gt_row gt_right\">110</td>\n<td headers=\"drat\" class=\"gt_row gt_right\">3.08 (3.215)</td>\n<td headers=\"qsec\" class=\"gt_row gt_right\">19.44</td>\n<td headers=\"vs\" class=\"gt_row gt_right\">1</td>\n<td headers=\"am\" class=\"gt_row gt_right\">0</td>\n<td headers=\"gear\" class=\"gt_row gt_right\">3</td>\n<td headers=\"carb\" class=\"gt_row gt_right\">1</td></tr>\n <tr><td headers=\"mpg\" class=\"gt_row gt_right\">18.7</td>\n<td headers=\"cyl\" class=\"gt_row gt_right\">8</td>\n<td headers=\"disp\" class=\"gt_row gt_right\">360</td>\n<td headers=\"hp\" class=\"gt_row gt_right\">175</td>\n<td headers=\"drat\" class=\"gt_row gt_right\">3.15</td>\n<td headers=\"qsec\" class=\"gt_row gt_right\">17.02</td>\n<td headers=\"vs\" class=\"gt_row gt_right\">0</td>\n<td headers=\"am\" class=\"gt_row gt_right\">0</td>\n<td headers=\"gear\" class=\"gt_row gt_right\">3</td>\n<td headers=\"carb\" class=\"gt_row gt_right\">2</td></tr>\n </tbody>\n \n \n</table>"

# cols_merge_uncert() works correctly
# cols_merge_uncert() works with row groups

Code
.
Expand All @@ -47,7 +47,7 @@
Output
[1] "<table class=\"gt_table\" data-quarto-disable-processing=\"false\" data-quarto-bootstrap=\"false\">\n <thead>\n <tr class=\"gt_col_headings\">\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"value\">value</th>\n </tr>\n </thead>\n <tbody class=\"gt_table_body\">\n <tr><td headers=\"value\" class=\"gt_row gt_right\">34.5<span style=\"display:inline-block;line-height:1em;text-align:right;font-size:60%;vertical-align:-0.25em;margin-left:0.1em;\">+1.8<br>−2.1</span></td></tr>\n <tr><td headers=\"value\" class=\"gt_row gt_right\">29.2</td></tr>\n <tr><td headers=\"value\" class=\"gt_row gt_right\">36.3</td></tr>\n <tr><td headers=\"value\" class=\"gt_row gt_right\">31.6<span style=\"display:inline-block;line-height:1em;text-align:right;font-size:60%;vertical-align:-0.25em;margin-left:0.1em;\">+NA<br>−1.8</span></td></tr>\n <tr><td headers=\"value\" class=\"gt_row gt_right\">28.5</td></tr>\n <tr><td headers=\"value\" class=\"gt_row gt_right\">30.9</td></tr>\n <tr><td headers=\"value\" class=\"gt_row gt_right\">NA</td></tr>\n <tr><td headers=\"value\" class=\"gt_row gt_right\">NA</td></tr>\n <tr><td headers=\"value\" class=\"gt_row gt_right\">Inf</td></tr>\n <tr><td headers=\"value\" class=\"gt_row gt_right\">30.0 ± 0.0</td></tr>\n <tr><td headers=\"value\" class=\"gt_row gt_right\">32.0<span style=\"display:inline-block;line-height:1em;text-align:right;font-size:60%;vertical-align:-0.25em;margin-left:0.1em;\">+0.0<br>−0.1</span></td></tr>\n <tr><td headers=\"value\" class=\"gt_row gt_right\">34.0</td></tr>\n <tr><td headers=\"value\" class=\"gt_row gt_right\">NaN</td></tr>\n </tbody>\n \n \n</table>"

# cols_merge_range() works correctly
# cols_merge_range() produces the correct output

Code
.
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ tidy_grepl <- function(x, pattern) {
)
}

expect_merge_locale_sep <- function(locale = NULL, global_locale = NULL, sep = NULL, expected_sep) {
tbl <- data.frame(
col_1 = 1,
col_2 = 2,
col_3 = 3,
col_4 = 4
)
gt_tbl <- gt(tbl, locale = global_locale)
merged_gt <- cols_merge_range(gt_tbl, col_begin = "col_1", col_end = "col_2", locale = locale, sep = sep)
actual_merge_sep <- dt_col_merge_get(merged_gt)[[1]]$sep
expect_equal(actual_merge_sep, expected_sep, label = paste0("locale = ", locale))
}

# Create a shortened version of `mtcars`
mtcars_short <- datasets::mtcars[1:5, ]

Expand Down
Loading

0 comments on commit a46ff48

Please sign in to comment.