Skip to content

Commit b553f2b

Browse files
authored
Merge branch 'main' into remove_lookups@main
2 parents 248cc29 + 5385b1a commit b553f2b

File tree

5 files changed

+87
-10
lines changed

5 files changed

+87
-10
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: rtables
22
Title: Reporting Tables
3-
Version: 0.6.11.9002
4-
Date: 2025-01-27
3+
Version: 0.6.11.9004
4+
Date: 2025-02-06
55
Authors@R: c(
66
person("Gabriel", "Becker", , "[email protected]", role = "aut",
77
comment = "Original creator of the package"),

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
## rtables 0.6.11.9002
1+
## rtables 0.6.11.9004
22

33
### New Features
44
* Added `stat_string` to `as_result_df(make_ard = TRUE)` to preserve the original string representation of the statistics.
55
* Added `add_tbl_name_split` to `as_result_df()` to handle split levels constituted by different table names.
66

77
### Bug Fixes
88
* Fixed issue with `split_cols_by_multivar()` when having more than one value. Now `as_result_df(make_ard = TRUE)` adds a predefined split name for each of the `multivar` splits.
9+
* Fixed bug happening when format functions were changing the number of printed values. Now `as_result_df(make_ard = TRUE)` uses the cell values for `stat_strings` for these exceptions.
910

1011
## rtables 0.6.11
1112

R/tt_as_df.R

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#' @param add_tbl_name_split (`flag`)\cr when `TRUE` and when the table has more than one
2121
#' `analyze(table_names = "<diff_names>")`, the table names will be present as a group split named
2222
#' `"<analysis_spl_tbl_name>"`.
23+
#' @param verbose (`flag`)\cr when `TRUE`, the function will print additional information for
24+
#' `data_format != "full_precision"`.
2325
#' @param ... additional arguments passed to spec-specific result data frame function (`spec`).
2426
#'
2527
#' @return
@@ -46,6 +48,7 @@ as_result_df <- function(tt, spec = NULL,
4648
keep_label_rows = FALSE,
4749
add_tbl_name_split = FALSE,
4850
simplify = FALSE,
51+
verbose = FALSE,
4952
...) {
5053
data_format <- data_format[[1]]
5154
checkmate::assert_class(tt, "VTableTree")
@@ -56,6 +59,7 @@ as_result_df <- function(tt, spec = NULL,
5659
checkmate::assert_flag(keep_label_rows)
5760
checkmate::assert_flag(simplify)
5861
checkmate::assert_flag(add_tbl_name_split)
62+
checkmate::assert_flag(verbose)
5963

6064
if (nrow(tt) == 0) {
6165
return(sanitize_table_struct(tt))
@@ -70,7 +74,7 @@ as_result_df <- function(tt, spec = NULL,
7074
if (is.null(spec)) {
7175
# raw values
7276
rawvals <- cell_values(tt)
73-
cellvals <- .make_df_from_raw_data(rawvals, nr = nrow(tt), nc = ncol(tt))
77+
cellvals_init <- .make_df_from_raw_data(rawvals, nr = nrow(tt), nc = ncol(tt))
7478

7579
if (data_format %in% c("strings", "numeric")) {
7680
# we keep previous calculations to check the format of the data
@@ -80,15 +84,17 @@ as_result_df <- function(tt, spec = NULL,
8084
mf_result_numeric <- .make_numeric_char_mf(mf_result_chars)
8185
mf_result_chars <- as.data.frame(mf_result_chars)
8286
mf_result_numeric <- as.data.frame(mf_result_numeric)
83-
if (!setequal(dim(mf_result_numeric), dim(cellvals)) || !setequal(dim(mf_result_chars), dim(cellvals))) {
87+
cond1 <- !setequal(dim(mf_result_chars), dim(cellvals_init))
88+
cond2 <- !setequal(dim(mf_result_numeric), dim(cellvals_init))
89+
if (cond1 || cond2) {
8490
stop(
8591
"The extracted numeric data.frame does not have the same dimension of the",
8692
" cell values extracted with cell_values(). This is a bug. Please report it."
8793
) # nocov
8894
}
8995

90-
colnames(mf_result_chars) <- colnames(cellvals)
91-
colnames(mf_result_numeric) <- colnames(cellvals)
96+
colnames(mf_result_chars) <- colnames(cellvals_init)
97+
colnames(mf_result_numeric) <- colnames(cellvals_init)
9298
if (data_format == "strings") {
9399
cellvals <- mf_result_chars
94100
if (isTRUE(make_ard)) {
@@ -101,6 +107,41 @@ as_result_df <- function(tt, spec = NULL,
101107
cellvals <- mf_result_numeric
102108
}
103109
}
110+
111+
diff_in_cellvals <- length(unlist(cellvals_init)) - length(unlist(cellvals))
112+
if (make_ard && abs(diff_in_cellvals) > 0) {
113+
warning_msg <- paste0(
114+
"Found ", abs(diff_in_cellvals), " cell values that differ from ",
115+
"printed values. This is possibly related to conditional formatting. "
116+
)
117+
118+
# number of values difference mask between cellvals and cellvals_init (TRUE if different)
119+
dmc <- lengths(unlist(cellvals, recursive = FALSE)) != lengths(unlist(cellvals_init, recursive = FALSE))
120+
dmc <- matrix(dmc, nrow = nrow(cellvals), ncol = ncol(cellvals))
121+
122+
# Mainly used for debugging
123+
selected_rows_to_print <- mf_strings(mf_tt)[-seq_len(mf_nlheader(mf_tt)), , drop = FALSE]
124+
selected_rows_to_print <- cbind(
125+
which(apply(dmc, 1, any, simplify = TRUE)),
126+
as.data.frame(selected_rows_to_print[apply(dmc, 1, any), , drop = FALSE])
127+
)
128+
colnames(selected_rows_to_print) <- c("row_number", "row_name", colnames(cellvals_init))
129+
warning_msg <- if (verbose) {
130+
paste0(
131+
warning_msg,
132+
"\n",
133+
"The following row names were modified: ",
134+
paste(selected_rows_to_print$row_name, sep = ", ", collapse = ", "),
135+
"\n"
136+
)
137+
} else {
138+
paste0(warning_msg, "To see the affected row names use `verbose = TRUE`.")
139+
}
140+
warning(warning_msg)
141+
cellvals[dmc] <- cellvals_init[dmc]
142+
}
143+
} else {
144+
cellvals <- cellvals_init
104145
}
105146

106147
rdf <- make_row_df(tt)
@@ -115,7 +156,11 @@ as_result_df <- function(tt, spec = NULL,
115156
# Correcting maxlen for even number of paths (only multianalysis diff table names)
116157
maxlen <- max(lengths(df$path))
117158
if (maxlen %% 2 != 0) {
118-
maxlen <- maxlen + 1
159+
maxlen <- if (add_tbl_name_split) {
160+
maxlen + 1
161+
} else {
162+
maxlen - 1
163+
}
119164
}
120165

121166
# Loop for metadata (path and details from make_row_df)
@@ -299,7 +344,7 @@ as_result_df <- function(tt, spec = NULL,
299344
if (!"already_done" %in% names(list(...))) {
300345
stat_string_ret <- as_result_df(
301346
tt = tt, spec = spec, data_format = "numeric",
302-
make_ard = TRUE, already_done = TRUE, ...
347+
make_ard = TRUE, already_done = TRUE, verbose = verbose, ...
303348
)
304349
ret_w_cols <- cbind(ret_w_cols, "stat_string" = stat_string_ret$stat)
305350
}

man/data.frame_export.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-result_data_frame.R

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ test_that("Result Data Frame generation works v0", {
5454
analyze(c("AGE", "SEX"))
5555

5656
tbl4 <- build_table(lyt4, DM)
57-
result_df4 <- as_result_df(tbl4)
57+
result_df4 <- as_result_df(tbl4, add_tbl_name_split = TRUE)
5858

5959
expect_identical(
6060
names(result_df4),
@@ -533,6 +533,9 @@ test_that("make_ard works if string precision is needed", {
533533
})
534534

535535
test_that("make_ard works with split_cols_by_multivar", {
536+
skip_if_not_installed("dplyr")
537+
require(dplyr, quietly = TRUE)
538+
536539
# Regression test #970
537540
n <- 400
538541

@@ -563,3 +566,27 @@ test_that("make_ard works with split_cols_by_multivar", {
563566
expect_silent(out <- as_result_df(tbl, make_ard = TRUE))
564567
expect_true(all(out$group3 == "multivar_split1"))
565568
})
569+
test_that("make_ard works when printed format differs from cell values", {
570+
mean_sd_custom <- function(x, ...) {
571+
rcell(c(1, 2),
572+
label = "Mean (SD)", format = function(xf, ...) {
573+
return(as.character(xf[1]))
574+
}
575+
)
576+
}
577+
578+
test_out <- basic_table() %>%
579+
split_rows_by("ARM") %>%
580+
split_cols_by("ARM") %>%
581+
analyze(vars = "AGE", afun = mean_sd_custom) %>%
582+
build_table(DM)
583+
584+
expect_warning(
585+
out <- as_result_df(test_out, make_ard = TRUE, verbose = TRUE),
586+
"Found 9 cell"
587+
)
588+
expect_equal(
589+
out$stat,
590+
out$stat_string
591+
)
592+
})

0 commit comments

Comments
 (0)