-
-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1324 feature request varying decimal precision in a summary #1356
Draft
iaugusty
wants to merge
11
commits into
main
Choose a base branch
from
1324-feature-request-varying-decimal-precision-in-a_summary
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3080d20
draft proposal
iaugusty 3f10c4f
_ci_3d vars not defined in this branch
iaugusty 440ecf4
add new formatting function, similar to format_xx
iaugusty 9261ef4
update example
iaugusty bb7883c
add documentation files
iaugusty 4bcaa2f
Merge branch 'main' into 1324-feature-request-varying-decimal-precisi…
iaugusty a1994fa
update after merge from main
iaugusty a8bd470
[skip style] [skip vbump] Restyle files
github-actions[bot] 52056e4
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
github-actions[bot] 648d5b6
Merge branch 'main' into 1324-feature-request-varying-decimal-precisi…
Melkiades 84c581c
Merge branch 'main' into 1324-feature-request-varying-decimal-precisi…
shajoezhu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,3 +179,4 @@ Collate: | |
'utils_grid.R' | ||
'utils_rtables.R' | ||
'utils_split_funs.R' | ||
'xutils_custom_stats_formats_varying_dp.R' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -463,7 +463,53 @@ s_summary.logical <- function(x, | |
#' | ||
#' a_summary(rnorm(10), .N_col = 10, .N_row = 20, .var = "bla") | ||
#' a_summary(rnorm(10, 5, 1), .ref_group = rnorm(20, -5, 1), .var = "bla", compare = TRUE) | ||
#' a_summary( | ||
#' rnorm(10, 5, 1), | ||
#' .ref_group = rnorm(20, -5, 1), .var = "bla", compare = TRUE, | ||
#' .stats = "mean", | ||
#' fmts_df_var = "variant1", | ||
#' d = 2 | ||
#' ) | ||
#' | ||
#' x1 <- rnorm(10, 5, 1) | ||
#' xref <- rnorm(20, -5, 1) | ||
#' | ||
#' a_summary( | ||
#' x1, | ||
#' .ref_group = xref, .var = "bla", compare = TRUE, | ||
#' .stats = c("mean", "sd"), | ||
#' .formats = c("mean" = format_xx("xx.xxx"), "sd" = format_xx("xx.x")) | ||
#' ) | ||
#' a_summary( | ||
#' x1, | ||
#' .ref_group = xref, .var = "bla", compare = TRUE, | ||
#' .stats = "mean_sd", | ||
#' fmt_specs = list( | ||
#' fmts_df_var = "variant2", | ||
#' d = 1, | ||
#' formatting_function = "format_xx" | ||
#' ) | ||
#' ) | ||
#' a_summary( | ||
#' x1, | ||
#' .ref_group = xref, .var = "bla", compare = TRUE, | ||
#' .stats = c("mean", "mean_sd", "mean_pval") | ||
#' ) | ||
#' | ||
#' our_fmt_specs_variant <- list( | ||
#' fmts_df = tern_formats_custom_df(), | ||
#' fmts_df_var = "default", | ||
#' formatting_function = "format_xx_fixed_dp", | ||
#' d = 0 | ||
#' ) | ||
|
||
#' a_summary( | ||
#' x1, .ref_group = xref, .var = "bla", compare = TRUE, | ||
#' .stats = c("mean", "mean_sd", "mean_pval"), | ||
#' .formats = c("mean_sd" = "xx.d (xx.dxxxx)"), | ||
#' fmt_specs = our_fmt_specs_variant | ||
#' ) | ||
|
||
#' @export | ||
a_summary <- function(x, | ||
.N_col, # nolint | ||
|
@@ -479,6 +525,7 @@ a_summary <- function(x, | |
.indent_mods = NULL, | ||
na.rm = TRUE, # nolint | ||
na_str = default_na_str(), | ||
fmt_specs = default_fmt_specs, | ||
...) { | ||
extra_args <- list(...) | ||
if (is.numeric(x)) { | ||
|
@@ -507,7 +554,28 @@ a_summary <- function(x, | |
# Fill in with formatting defaults if needed | ||
met_grp <- paste0(c("analyze_vars", type), collapse = "_") | ||
.stats <- get_stats(met_grp, stats_in = .stats, add_pval = compare) | ||
.formats <- get_formats_from_stats(.stats, .formats) | ||
|
||
if (is.null(fmt_specs$fmts_df)) { | ||
.formats <- get_formats_from_stats(.stats, .formats) | ||
} else { | ||
d_actual <- derive_d_from_fmt_specs(fmt_specs, .df_row) | ||
|
||
# update the spec with the actual derived d | ||
fmt_specs$d <- d_actual | ||
|
||
# core function that does the conversion of the xx.d based formats to the actual format | ||
# note that is it most safe to apply formatting functions, as many of the final formats will not belong to | ||
# list_valid_format_labels() | ||
.formats_all <- get_formats_from_stats_custom( | ||
.stats, | ||
formats_in = .formats, | ||
### variant specific arguments | ||
fmts_specs = fmt_specs | ||
) | ||
.formats <- .formats_all$fmt | ||
.formats_char <- .formats_all$fmt_char | ||
} | ||
|
||
.indent_mods <- get_indents_from_stats(.stats, .indent_mods) | ||
|
||
lbls <- get_labels_from_stats(.stats, .labels) | ||
|
@@ -615,6 +683,59 @@ a_summary <- function(x, | |
#' ) %>% | ||
#' build_table(dt) | ||
#' | ||
#' # custom format | ||
#' our_fmt_specs_variant <- list( | ||
#' fmts_df = tern_formats_custom_df(), | ||
#' fmts_df_var = "variant2", | ||
#' formatting_function = "format_xx_fixed_dp", | ||
#' d = 0 | ||
#' ) | ||
#' | ||
#' dt <- data.frame("VAR" = c(0.001, 0.2, 0.0011000, 3, 4)) | ||
#' basic_table() %>% | ||
#' analyze_vars( | ||
#' vars = "VAR", | ||
#' .stats = c("n", "mean", "mean_sd", "range"), | ||
#' .formats = c("mean" = "xx.dxx"), | ||
#' fmt_specs = our_fmt_specs_variant, | ||
#' ) %>% | ||
#' build_table(dt) | ||
#' | ||
#' # custom format | ||
#' our_fmt_specs_variant2 <- list( | ||
#' fmts_df = tern_formats_custom_df(), | ||
#' fmts_df_var = "variant2", | ||
#' formatting_function = "format_xx_fixed_dp", | ||
#' d = "decimal" | ||
#' ) | ||
#' dt <- data.frame("VAR" = c(0.001, 0.2, 0.0011000, 3, 4), decimal = 2) | ||
#' basic_table() %>% | ||
#' analyze_vars( | ||
#' vars = "VAR", | ||
#' .stats = c("n", "mean", "mean_sd", "range"), | ||
#' .formats = c("mean" = "xx.dxxxxxx"), | ||
#' fmt_specs = our_fmt_specs_variant2, | ||
#' ) %>% | ||
#' build_table(dt) | ||
#' | ||
#' # custom format | ||
#' dt2 <- data.frame("VAR" = c(0.001, 0.2, 0.0011000, 3, 4, 0.002, 0.004, 0.006), decimal = c(rep(2, 4), rep(1, 4)), by = c(rep("by1", 4), rep("by2", 4))) | ||
#' our_fmt_specs_variant2 <- list( | ||
#' fmts_df = tern_formats_custom_df(), | ||
#' fmts_df_var = "variant2", | ||
#' formatting_function = "format_xx_fixed_dp", | ||
#' d = "decimal", | ||
#' d_cap = 3 | ||
#' ) | ||
#' basic_table() %>% | ||
#' split_rows_by("by") %>% | ||
#' analyze_vars( | ||
#' vars = "VAR", | ||
#' .stats = c("n", "mean", "mean_sd", "range"), | ||
#' fmt_specs = our_fmt_specs_variant2 | ||
#' ) %>% | ||
#' build_table(dt2) | ||
#' | ||
#' @export | ||
#' @order 2 | ||
analyze_vars <- function(lyt, | ||
|
@@ -630,8 +751,15 @@ analyze_vars <- function(lyt, | |
.stats = c("n", "mean_sd", "median", "range", "count_fraction"), | ||
.formats = NULL, | ||
.labels = NULL, | ||
.indent_mods = NULL) { | ||
extra_args <- list(.stats = .stats, na.rm = na.rm, na_str = na_str, ...) | ||
.indent_mods = NULL, | ||
# varying precision arguments | ||
fmt_specs = default_fmt_specs) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Everything should pass by |
||
extra_args <- list( | ||
.stats = .stats, na.rm = na.rm, na_str = na_str, | ||
fmt_specs = fmt_specs, | ||
... | ||
) | ||
|
||
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats | ||
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels | ||
if (!is.null(.indent_mods)) extra_args[[".indent_mods"]] <- .indent_mods | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.formats_char is not used?