Skip to content
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

Fmt fn arg #83

Merged
merged 18 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export(check_list_elements)
export(compute_formula_selector)
export(contains)
export(continuous_variable_summary_fns)
export(default_fmt_fns)
export(default_stat_labels)
export(ends_with)
export(eval_capture_conditions)
Expand Down
7 changes: 3 additions & 4 deletions R/ard_categorical.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ NULL
#' @export
ard_categorical <- function(data, variables, by = NULL, strata = NULL,
statistics = everything() ~ categorical_variable_summary_fns(),
denominator = NULL, fmt_fn = NULL,
denominator = NULL,
fmt_fn = everything() ~ default_fmt_fns(),
stat_labels = everything() ~ default_stat_labels()) {
# check inputs ---------------------------------------------------------------
check_not_missing(data)
Expand Down Expand Up @@ -108,6 +109,7 @@ ard_categorical <- function(data, variables, by = NULL, strata = NULL,
by = all_of(by),
strata = all_of(strata),
statistics = statistics,
fmt_fn = fmt_fn,
stat_labels = stat_labels
)

Expand All @@ -119,9 +121,6 @@ ard_categorical <- function(data, variables, by = NULL, strata = NULL,
# process the table() results and add to the ARD data frame ------------------
df_result_final <- .unnest_table_object(df_result, data)

# if user passed formatting functions, update data frame
df_result_final <- .update_with_fmt_fn(df_result_final, fmt_fn)

# merge in stat labels and format ARD for return -----------------------------
df_result_final |>
dplyr::arrange(dplyr::across(c(all_ard_groups(), all_ard_variables()))) |>
Expand Down
82 changes: 23 additions & 59 deletions R/ard_continuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,30 @@ ard_continuous <- function(data,
by = NULL,
strata = NULL,
statistics = everything() ~ continuous_variable_summary_fns(),
fmt_fn = NULL,
fmt_fn = everything() ~ default_fmt_fns(),
stat_labels = everything() ~ default_stat_labels()) {
# check inputs ---------------------------------------------------------------
check_not_missing(data)
check_not_missing(variables)
check_class_data_frame(data = data)
check_class(class = c("list", "formula"), statistics = statistics, allow_null = TRUE)
check_class(class = c("list", "formula"), stat_labels = stat_labels, allow_null = TRUE)
check_class(class = c("list", "formula"), fmt_fn = fmt_fn, allow_null = TRUE)

# process arguments ----------------------------------------------------------
data <- dplyr::ungroup(data)
process_selectors(data, variables = {{variables}}, by = {{by}}, strata = {{strata}})

process_formula_selectors(
data = data[variables],
statistics = statistics,
fmt_fn = fmt_fn
)
fill_formula_selectors(
data = data[variables],
statistics = formals(cards::ard_continuous)[["statistics"]] |> eval()
fmt_fn = fmt_fn,
stat_labels = stat_labels
)
process_formula_selectors(data = data[variables], stat_labels = stat_labels)
fill_formula_selectors(
data = data[variables],
statistics = formals(cards::ard_continuous)[["statistics"]] |> eval(),
fmt_fn = formals(cards::ard_continuous)[["fmt_fn"]] |> eval(),
stat_labels = formals(cards::ard_continuous)[["stat_labels"]] |> eval()
)

Expand All @@ -92,8 +92,11 @@ ard_continuous <- function(data,
# return empty tibble if no variables selected -------------------------------
if (rlang::is_empty(variables)) return(dplyr::tibble())

# final processing of fmt_fn -------------------------------------------------
df_fmt_fn <- .process_stat_arg(fmt_fn, col_name = "statistic_fmt_fn")

# final processing of stat labels -------------------------------------------------
df_stat_labels <- .process_stat_labels(stat_labels)
df_stat_labels <- .process_stat_arg(stat_labels, col_name = "stat_label")

# calculate statistics -------------------------------------------------------
df_nested <-
Expand Down Expand Up @@ -124,19 +127,16 @@ ard_continuous <- function(data,
by = c("variable", "stat_name")
) |>
dplyr::left_join(
.default_statistic_formatters(),
by = "stat_name"
df_fmt_fn,
by = c("variable", "stat_name")
) |>
dplyr::mutate(
stat_label = ifelse(is.na(.data$stat_label), .data$stat_name, .data$stat_label),
statistic_fmt_fn =
.data$statistic_fmt_fn |>
lapply(function(fn) fn %||% function(x) format(round5(x, digits = 0), nsmall = 0))
lapply(function(fn) fn %||% function(x) format(round5(x, digits = 1), nsmall = 1))
bzkrouse marked this conversation as resolved.
Show resolved Hide resolved
)

# if user passed formatting functions, update data frame
df_results <- .update_with_fmt_fn(df_results, fmt_fn)

# add meta data and class
df_results |>
dplyr::mutate(context = "continuous") |>
Expand All @@ -145,44 +145,6 @@ ard_continuous <- function(data,
{structure(., class = c("card", class(.)))}
}

#' Update Formatting Functions
#'
#' Updates the default formatting functions with those passed in this function.
#'
#' @param x a ARD object of class 'cards'
#' @inheritParams ard_continuous
#'
#' @keywords internal
#' @return a ARD object of class 'cards'
.update_with_fmt_fn <- function(x, fmt_fn) {
if (rlang::is_empty(fmt_fn)) return(x)

# recast the argument values as a data frame
df_fmt_fn <-
dplyr::tibble(variable = names(fmt_fn)) |>
dplyr::mutate(
data =
lapply(
.data$variable,
function(x) {
dplyr::tibble(
stat_name = names(fmt_fn[[x]]),
statistic_fmt_fn = fmt_fn[[x]]
)
}
)
) |>
tidyr::unnest(cols = "data")

x |>
dplyr::rows_update(
df_fmt_fn,
by = c("variable", "stat_name"),
unmatched = "ignore"
)
}


#' Calculate Continuous Statistics
#'
#' Calculate statistics and return in an ARD format
Expand Down Expand Up @@ -233,28 +195,28 @@ ard_continuous <- function(data,

#' Process Statistic Labels
#'
#' @param stat_labels named list
#'
#' @param stat_arg_list named list
#' @param col_name column name in the ARD to make for the stat argument
#' @return named list
#' @keywords internal
#' @examples
#' list(AGE = list(c("N", "n") ~ "{n} / {N}")) |>
#' cards:::.process_stat_labels()
.process_stat_labels <- function(stat_labels){
#' cards:::.process_stat_arg(col_name = "stat_label")
.process_stat_arg <- function(stat_arg_list, col_name){
ddsjoberg marked this conversation as resolved.
Show resolved Hide resolved

# create the tibble of stat names and labels 1 variable at a time
# both the stat_labels and statistics are a named (variable-level) list of stat info
stat_labels <- map(stat_labels, function(x){
args_tbl <- map(stat_arg_list, function(x, y){
bzkrouse marked this conversation as resolved.
Show resolved Hide resolved
# handle the named list or formula & create tibble
compute_formula_selector(data=NULL, x=x) %>%
{dplyr::tibble(
stat_name = names(.),
stat_label = unlist(.) |> unname()
!!col_name := unlist(.) |> unname()
)}
})

# stack result
stat_labels |>
args_tbl |>
dplyr::bind_rows(.id = "variable")

}
Expand Down Expand Up @@ -285,6 +247,8 @@ ard_continuous <- function(data,
}




#' Default formatting functions
#'
#' Global default is to round the statistic to one decimal place for
Expand Down
3 changes: 2 additions & 1 deletion R/ard_dichotomous.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
ard_dichotomous <- function(data, variables, by = NULL, strata = NULL,
values = max_value(data[variables]),
statistics = everything() ~ categorical_variable_summary_fns(),
denominator = NULL, fmt_fn = NULL,
denominator = NULL,
fmt_fn = everything() ~ default_fmt_fns(),
stat_labels = everything() ~ default_stat_labels()) {
# check inputs ---------------------------------------------------------------
check_not_missing(data)
Expand Down
2 changes: 1 addition & 1 deletion R/ard_missing.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ard_missing <- function(data,
variables,
by = NULL,
statistics = everything() ~ missing_variable_summary_fns(),
fmt_fn = NULL,
fmt_fn = everything() ~ default_fmt_fns(),
stat_labels = everything() ~ default_stat_labels()) {

# process variable inputs ----------------------------------------------------
Expand Down
58 changes: 58 additions & 0 deletions R/default_stat_args.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#' Defaults for statistical arguments
#'
#' @description
#' -`default_stat_labels()` returns a named list of statistics labels
#'
#' - `default_fmt_fns()` returns a named list of formatting functions to be applied in lieu
#' of the global default, which is to round to one decimal place.
#'
#' @return named list of values
#'
#' @examples
#' # stat labels
#' default_stat_labels()
#'
#' # formatting functions
#' default_fmt_fns()
#'
#' @rdname default_stat_args
#' @export
default_stat_labels <- function() {

list(
mean = "Mean",
sd = "SD",
var = "Variance",
median = "Median",
p25 = "25th Percentile",
p75 = "75th Percentile",
min = "Min",
max = "Max",

n = "n",
N = "N",
length = "Vector Length",
p = "%",
p_cell = "%",

N_obs = "Vector Length",
N_miss = "N Missing",
N_nonmiss = "N Non-missing",
p_miss = "% Missing",
p_nonmiss = "% Non-missing"
)
}

#' @rdname default_stat_args
#' @export
default_fmt_fns <- function(){

list(
n = function(x) format(round5(x, digits = 0), nsmall = 0),
N_miss = function(x) format(round5(x, digits = 0), nsmall = 0),
length = function(x) format(round5(x, digits = 0), nsmall = 0),
p = function(x) format(round5(x * 100, digits = 1), nsmall = 1),
p_cell = function(x) format(round5(x * 100, digits = 1), nsmall = 1)
)

}
34 changes: 0 additions & 34 deletions R/default_stat_labels.R

This file was deleted.

2 changes: 1 addition & 1 deletion man/ard_categorical.Rd

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

2 changes: 1 addition & 1 deletion man/ard_continuous.Rd

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

2 changes: 1 addition & 1 deletion man/ard_dichotomous.Rd

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

2 changes: 1 addition & 1 deletion man/ard_missing.Rd

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

29 changes: 29 additions & 0 deletions man/default_stat_args.Rd

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

17 changes: 0 additions & 17 deletions man/default_stat_labels.Rd

This file was deleted.

Loading
Loading