From d66214554a3ad042551b2f840b652b8b0aa1f9de Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Sun, 24 Sep 2023 18:07:05 +0200 Subject: [PATCH] YAGNI grouped_list helper --- R/ggbarstats.R | 6 +++--- R/ggbetweenstats.R | 4 ++-- R/ggcoefstats.R | 6 +++--- R/ggdotplotstats.R | 4 ++-- R/gghistostats.R | 6 +++--- R/ggpiestats.R | 4 ++-- R/ggscatterstats.R | 4 ++-- R/ggwithinstats.R | 4 ++-- R/utils.R | 13 ++++--------- codemeta.json | 2 +- man/dot-grouped_list.Rd | 2 +- 11 files changed, 25 insertions(+), 30 deletions(-) diff --git a/R/ggbarstats.R b/R/ggbarstats.R index c707f2683..c64596e05 100644 --- a/R/ggbarstats.R +++ b/R/ggbarstats.R @@ -106,12 +106,12 @@ ggbarstats <- function(data, ) subtitle_df <- .eval_f(contingency_table, !!!.f.args, type = type) - subtitle <- extract_expression(subtitle_df) + subtitle <- .extract_expression(subtitle_df) # Bayes Factor caption if (type != "bayes" && bf.message && isFALSE(paired)) { caption_df <- .eval_f(contingency_table, !!!.f.args, type = "bayes") - caption <- extract_expression(caption_df) + caption <- .extract_expression(caption_df) } } @@ -130,7 +130,7 @@ ggbarstats <- function(data, plotBar <- ggplot(descriptive_df, aes({{ y }}, perc, fill = {{ x }})) + geom_bar(stat = "identity", position = "fill", color = "black") + scale_y_continuous( - labels = function(x) paste0(x * 100L, "%"), + labels = ~ insight::format_percent(., digits = 0L), breaks = seq(from = 0.0, to = 1.0, by = 0.10), minor_breaks = seq(from = 0.05, to = 0.95, by = 0.10) ) + diff --git a/R/ggbetweenstats.R b/R/ggbetweenstats.R index 50881e819..588801229 100644 --- a/R/ggbetweenstats.R +++ b/R/ggbetweenstats.R @@ -220,11 +220,11 @@ ggbetweenstats <- function(data, .f <- .f_switch(test) subtitle_df <- .eval_f(.f, !!!.f.args, type = type) - subtitle <- extract_expression(subtitle_df) + subtitle <- .extract_expression(subtitle_df) if (type == "parametric" && bf.message) { caption_df <- .eval_f(.f, !!!.f.args, type = "bayes") - caption <- extract_expression(caption_df) + caption <- .extract_expression(caption_df) } } diff --git a/R/ggcoefstats.R b/R/ggcoefstats.R index 4f181726a..074bfd926 100644 --- a/R/ggcoefstats.R +++ b/R/ggcoefstats.R @@ -239,7 +239,7 @@ ggcoefstats <- function(x, if (!is.null(glance_df) && all(c("AIC", "BIC") %in% names(glance_df))) { glance_df %<>% mutate(expression = list(parse(text = glue("list(AIC=='{format_value(AIC, 0L)}', BIC=='{format_value(BIC, 0L)}')")))) - caption <- extract_expression(glance_df) + caption <- .extract_expression(glance_df) } # meta analysis ------------------------- @@ -249,12 +249,12 @@ ggcoefstats <- function(x, # frequentist subtitle_df <- meta_analysis(tidy_df, type = meta.type, k = k) - subtitle <- extract_expression(subtitle_df) + subtitle <- .extract_expression(subtitle_df) # Bayesian if (meta.type == "parametric" && bf.message) { caption_df <- suppressWarnings(meta_analysis(tidy_df, type = "bayes", k = k)) - caption <- extract_expression(caption_df) + caption <- .extract_expression(caption_df) } } diff --git a/R/ggdotplotstats.R b/R/ggdotplotstats.R index c4e1b0645..68f8cab09 100644 --- a/R/ggdotplotstats.R +++ b/R/ggdotplotstats.R @@ -106,11 +106,11 @@ ggdotplotstats <- function(data, ) subtitle_df <- .eval_f(one_sample_test, !!!.f.args, type = type) - subtitle <- extract_expression(subtitle_df) + subtitle <- .extract_expression(subtitle_df) if (type == "parametric" && bf.message) { caption_df <- .eval_f(one_sample_test, !!!.f.args, type = "bayes") - caption <- extract_expression(caption_df) + caption <- .extract_expression(caption_df) } } diff --git a/R/gghistostats.R b/R/gghistostats.R index 88f6b7555..4ff32b26b 100644 --- a/R/gghistostats.R +++ b/R/gghistostats.R @@ -110,12 +110,12 @@ gghistostats <- function(data, # subtitle with statistical results subtitle_df <- .eval_f(one_sample_test, !!!.f.args, type = type) - subtitle <- extract_expression(subtitle_df) + subtitle <- .extract_expression(subtitle_df) # BF message if (type == "parametric" && bf.message) { caption_df <- .eval_f(one_sample_test, !!!.f.args, type = "bayes") - caption <- extract_expression(caption_df) + caption <- .extract_expression(caption_df) } } @@ -131,7 +131,7 @@ gghistostats <- function(data, scale_y_continuous( sec.axis = sec_axis( trans = ~ . / nrow(data), - labels = function(x) paste0(x * 100, "%"), + labels = function(x) insight::format_percent(x, digits = 0L), name = "proportion" ) ) + diff --git a/R/ggpiestats.R b/R/ggpiestats.R index d9a68d1ec..3985e3389 100644 --- a/R/ggpiestats.R +++ b/R/ggpiestats.R @@ -138,12 +138,12 @@ ggpiestats <- function(data, ) subtitle_df <- .eval_f(contingency_table, !!!.f.args, type = type) - subtitle <- extract_expression(subtitle_df) + subtitle <- .extract_expression(subtitle_df) # Bayes Factor caption if (type != "bayes" && bf.message && isFALSE(paired)) { caption_df <- .eval_f(contingency_table, !!!.f.args, type = "bayes") - caption <- extract_expression(caption_df) + caption <- .extract_expression(caption_df) } } diff --git a/R/ggscatterstats.R b/R/ggscatterstats.R index 1dda80df4..b271f0fc6 100644 --- a/R/ggscatterstats.R +++ b/R/ggscatterstats.R @@ -130,12 +130,12 @@ ggscatterstats <- function(data, ) subtitle_df <- .eval_f(corr_test, !!!.f.args, type = type) - subtitle <- extract_expression(subtitle_df) + subtitle <- .extract_expression(subtitle_df) # BF message for null hypothesis support if (type == "parametric" && bf.message) { caption_df <- .eval_f(corr_test, !!!.f.args, type = "bayes") - caption <- extract_expression(caption_df) + caption <- .extract_expression(caption_df) } } diff --git a/R/ggwithinstats.R b/R/ggwithinstats.R index 9173a1643..02f418a6c 100644 --- a/R/ggwithinstats.R +++ b/R/ggwithinstats.R @@ -151,13 +151,13 @@ ggwithinstats <- function(data, # styler: off .f <- .f_switch(test) subtitle_df <- .eval_f(.f, !!!.f.args, type = type) - subtitle <- extract_expression(subtitle_df) + subtitle <- .extract_expression(subtitle_df) # styler: on if (type == "parametric" && bf.message) { # styler: off caption_df <- .eval_f(.f, !!!.f.args, type = "bayes") - caption <- extract_expression(caption_df) + caption <- .extract_expression(caption_df) # styler: on } } diff --git a/R/utils.R b/R/utils.R index bca641943..88a75e90d 100644 --- a/R/utils.R +++ b/R/utils.R @@ -12,14 +12,9 @@ #' @examplesIf identical(Sys.getenv("NOT_CRAN"), "true") #' ggstatsplot:::.grouped_list(ggplot2::msleep, grouping.var = vore) #' @keywords internal -.grouped_list <- function(data, grouping.var = NULL) { - data <- as_tibble(data) - - if (quo_is_null(enquo(grouping.var))) { - return(data) - } - - split(data, f = new_formula(NULL, enquo(grouping.var)), drop = TRUE) %>% +.grouped_list <- function(data, grouping.var) { + as_tibble(data) %>% + split(f = new_formula(NULL, enquo(grouping.var)), drop = TRUE) %>% list(data = ., title = names(.)) } @@ -65,4 +60,4 @@ #' @noRd -extract_expression <- function(data) purrr::pluck(data, "expression", 1L, .default = NULL) +.extract_expression <- function(data) purrr::pluck(data, "expression", 1L, .default = NULL) diff --git a/codemeta.json b/codemeta.json index 4cfb6c273..15444dd50 100644 --- a/codemeta.json +++ b/codemeta.json @@ -525,7 +525,7 @@ }, "SystemRequirements": null }, - "fileSize": "9389.014KB", + "fileSize": "9422.075KB", "citation": [ { "@type": "ScholarlyArticle", diff --git a/man/dot-grouped_list.Rd b/man/dot-grouped_list.Rd index bb7fc6ed0..d1bfebf7e 100644 --- a/man/dot-grouped_list.Rd +++ b/man/dot-grouped_list.Rd @@ -4,7 +4,7 @@ \alias{.grouped_list} \title{Split data frame into a list by grouping variable} \usage{ -.grouped_list(data, grouping.var = NULL) +.grouped_list(data, grouping.var) } \arguments{ \item{data}{A data frame (or a tibble) from which variables specified are to