Skip to content

Commit

Permalink
final fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Melkiades committed Sep 6, 2023
1 parent 4ba5455 commit fc646dd
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 11 deletions.
17 changes: 12 additions & 5 deletions R/analyze_variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,18 @@ a_summary_internal <- function(x,
na.rm, # nolint
na_level,
...) {
if (is.numeric(x)) {
type <- "numeric"
if (!is.null(.stats) && any(grepl("^pval", .stats))) {
.stats[grepl("^pval", .stats)] <- "pval" # tmp fix xxx
}
} else {
type <- "counts"
if (!is.null(.stats) && any(grepl("^pval", .stats))) {
.stats[grepl("^pval", .stats)] <- "pval_counts" # tmp fix xxx
}
}

# If one col has NA vals, must add NA row to other cols (using placeholder lvl `fill-na-level`)
if (any(is.na(.df_row[[.var]])) && !any(is.na(x)) && !na.rm) levels(x) <- c(levels(x), "fill-na-level")

Expand Down Expand Up @@ -557,11 +569,6 @@ a_summary <- function(x,
na.rm = TRUE, # nolint
na_level = NA_character_,
...) {
type <- if (is.numeric(x)) {
"numeric"
} else {
"counts"
}
a_summary_internal(
x = x,
.N_col = .N_col,
Expand Down
2 changes: 1 addition & 1 deletion R/compare_variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ compare_vars <- function(lyt,
.formats = NULL,
.labels = NULL,
.indent_mods = NULL) {
.stats <- .stats[!grepl("pval", .stats)] # tmp fix xxx

extra_args <- list(.stats = .stats, na.rm = na.rm, na_level = na_level, compare = TRUE, ...)
if (!is.null(.formats)) extra_args[[".formats"]] <- .formats
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels
Expand Down
10 changes: 5 additions & 5 deletions R/utils_defaults_handling.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,6 @@ get_stats <- function(method_groups, type = NULL, stats_in = NULL, add_pval = FA
add_pval <- TRUE
}

# Filtering for stats_in (character vector)
if (!is.null(stats_in)) {
out <- intersect(stats_in, out) # It orders them too
}

# Mainly used in "analyze_vars" but it could be necessary elsewhere
if (isTRUE(add_pval)) {
if (length(type) > 1) {
Expand All @@ -150,6 +145,11 @@ get_stats <- function(method_groups, type = NULL, stats_in = NULL, add_pval = FA
}
}

# Filtering for stats_in (character vector)
if (!is.null(stats_in)) {
out <- intersect(stats_in, out) # It orders them too
}

# If intersect did not find matches (and no pval?) -> error
if (length(out) == 0) {
stop(
Expand Down
93 changes: 93 additions & 0 deletions tests/testthat/_snaps/utils_defaults_handling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# get_stats works as expected for defaults

Code
res
Output
[1] "count" "count_fraction_fixed_dp"
[3] "fraction"

---

Code
res
Output
[1] "unique" "nonunique" "unique_count"

---

Code
res
Output
[1] "n" "count" "count_fraction" "n_blq"

---

Code
res
Output
[1] "n" "sum" "mean" "sd" "se"
[6] "mean_sd" "mean_se" "mean_ci" "mean_sei" "mean_sdi"
[11] "mean_pval" "median" "mad" "median_ci" "quantiles"
[16] "iqr" "range" "min" "max" "median_range"
[21] "cv" "geom_mean" "geom_mean_ci" "geom_cv"

# get_format_from_stats works as expected

Code
res
Output
$count
[1] "xx."
$count_fraction_fixed_dp
function(x, ...) {
attr(x, "label") <- NULL
if (any(is.na(x))) {
return("NA")
}
checkmate::assert_vector(x)
checkmate::assert_integerish(x[1])
assert_proportion_value(x[2], include_boundaries = TRUE)
result <- if (x[1] == 0) {
"0"
} else if (x[2] == 1) {
sprintf("%d (100%%)", x[1])
} else {
sprintf("%d (%.1f%%)", x[1], x[2] * 100)
}
return(result)
}
<environment: namespace:tern>
$fraction
function(x, ...) {
attr(x, "label") <- NULL
checkmate::assert_vector(x)
checkmate::assert_count(x["num"])
checkmate::assert_count(x["denom"])
result <- if (x["num"] == 0) {
paste0(x["num"], "/", x["denom"])
} else {
paste0(
x["num"], "/", x["denom"],
" (", sprintf("%.1f", round(x["num"] / x["denom"] * 100, 1)), "%)"
)
}
return(result)
}
<environment: namespace:tern>

# get_label_from_stats works as expected

Code
res
Output
count count_fraction_fixed_dp fraction
"count" "" ""

0 comments on commit fc646dd

Please sign in to comment.