Skip to content

Commit

Permalink
use custom function to eval stats functions
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil committed Jul 10, 2021
1 parent f37fb66 commit 5299455
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 65 deletions.
9 changes: 9 additions & 0 deletions R/extract_stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,12 @@ extract_stats <- function(p, ...) {
one_sample_data = tryCatch(p$plot_env$onesample_df, error = function(e) NULL)
)
}

#' @noRd

eval_f <- function(.f, ...) {
tryCatch(
suppressWarnings(suppressMessages(rlang::exec(.f, ...))),
error = function(e) NULL
)
}
10 changes: 2 additions & 8 deletions R/ggbarstats.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,12 @@ ggbarstats <- function(data,
top.text = caption
)

subtitle_df <- tryCatch(rlang::exec(contingency_table, !!!.f.args, type = type),
error = function(e) NULL
)

subtitle_df <- eval_f(contingency_table, !!!.f.args, type = type)
if (!is.null(subtitle_df)) subtitle <- subtitle_df$expression[[1]]

# preparing Bayes Factor caption
if (type != "bayes" && isTRUE(bf.message) && isFALSE(paired)) {
caption_df <- tryCatch(rlang::exec(contingency_table, !!!.f.args, type = "bayes"),
error = function(e) NULL
)

caption_df <- eval_f(contingency_table, !!!.f.args, type = "bayes")
if (!is.null(caption_df)) caption <- caption_df$expression[[1]]
}
}
Expand Down
12 changes: 2 additions & 10 deletions R/ggbetweenstats.R
Original file line number Diff line number Diff line change
Expand Up @@ -259,20 +259,12 @@ ggbetweenstats <- function(data,
)

.f <- function_switch(test)

subtitle_df <- tryCatch(
rlang::exec(.f, !!!.f.args, type = type),
error = function(e) NULL
)

subtitle_df <- eval_f(.f, !!!.f.args, type = type)
subtitle <- if (!is.null(subtitle_df)) subtitle_df$expression[[1]]

# preparing the Bayes factor message
if (type == "parametric" && isTRUE(bf.message)) {
caption_df <- tryCatch(rlang::exec(.f, !!!.f.args, type = "bayes"),
error = function(e) NULL
)

caption_df <- eval_f(.f, !!!.f.args, type = "bayes")
caption <- if (!is.null(caption_df)) caption_df$expression[[1]]
}
}
Expand Down
20 changes: 7 additions & 13 deletions R/ggdotplotstats.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ ggdotplotstats <- function(data,
# ensure the variables work quoted or unquoted
c(x, y) %<-% c(rlang::ensym(x), rlang::ensym(y))

# --------------------------- data preparation ----------------------------
# data -----------------------------------

# creating a dataframe
data %<>%
Expand All @@ -99,7 +99,7 @@ ggdotplotstats <- function(data,
rank = dplyr::row_number()
)

# ================ stats labels ==========================================
# statistical analysis ------------------------------------------

if (isTRUE(results.subtitle)) {
.f.args <- list(
Expand All @@ -115,18 +115,12 @@ ggdotplotstats <- function(data,
)

# preparing the subtitle with statistical results
subtitle_df <- tryCatch(rlang::exec(one_sample_test, !!!.f.args, type = type),
error = function(e) NULL
)

subtitle_df <- eval_f(one_sample_test, !!!.f.args, type = type)
subtitle <- if (!is.null(subtitle_df)) subtitle_df$expression[[1]]

# preparing the BF message
if (type == "parametric" && isTRUE(bf.message)) {
caption_df <- tryCatch(rlang::exec(one_sample_test, !!!.f.args, type = "bayes"),
error = function(e) NULL
)

caption_df <- eval_f(one_sample_test, !!!.f.args, type = "bayes")
caption <- if (!is.null(caption_df)) caption_df$expression[[1]]
}
}
Expand All @@ -139,7 +133,7 @@ ggdotplotstats <- function(data,
))
}

# ------------------------------ basic plot ----------------------------
# plot -----------------------------------

# creating the basic plot
plot <- ggplot2::ggplot(data, mapping = ggplot2::aes({{ x }}, y = rank)) +
Expand All @@ -154,7 +148,7 @@ ggdotplotstats <- function(data,
labels = 25 * 0:4
)
)
# ---------------- centrality tagging -------------------------------------
# centrality plotting -------------------------------------

# using custom function for adding labels
if (isTRUE(centrality.plotting)) {
Expand All @@ -168,7 +162,7 @@ ggdotplotstats <- function(data,
)
}

# ------------------------ annotations and themes -------------------------
# annotations -------------------------

# specifying theme and labels for the final plot
plot +
Expand Down
14 changes: 4 additions & 10 deletions R/gghistostats.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ gghistostats <- function(data,
x_vec <- df %>% dplyr::pull({{ x }})
if (is.null(binwidth)) binwidth <- (max(x_vec) - min(x_vec)) / sqrt(length(x_vec))

# subtitle/caption preparation ------------------------
# statistical analysis ------------------------------------------

if (isTRUE(results.subtitle)) {
.f.args <- list(
Expand All @@ -121,18 +121,12 @@ gghistostats <- function(data,
)

# preparing the subtitle with statistical results
subtitle_df <- tryCatch(rlang::exec(one_sample_test, !!!.f.args, type = type),
error = function(e) NULL
)

subtitle_df <- eval_f(one_sample_test, !!!.f.args, type = type)
subtitle <- if (!is.null(subtitle_df)) subtitle_df$expression[[1]]

# preparing the BF message
if (type == "parametric" && isTRUE(bf.message)) {
caption_df <- tryCatch(rlang::exec(one_sample_test, !!!.f.args, type = "bayes"),
error = function(e) NULL
)

caption_df <- eval_f(one_sample_test, !!!.f.args, type = "bayes")
caption <- if (!is.null(caption_df)) caption_df$expression[[1]]
}
}
Expand All @@ -145,7 +139,7 @@ gghistostats <- function(data,
))
}

# ============================= plot ====================================
# plot -----------------------------------

# adding axes info
plot <- ggplot2::ggplot(df, mapping = ggplot2::aes(x = {{ x }})) +
Expand Down
10 changes: 2 additions & 8 deletions R/ggpiestats.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,12 @@ ggpiestats <- function(data,
top.text = caption
)

subtitle_df <- tryCatch(rlang::exec(contingency_table, !!!.f.args, type = type),
error = function(e) NULL
)

subtitle_df <- eval_f(contingency_table, !!!.f.args, type = type)
if (!is.null(subtitle_df)) subtitle <- subtitle_df$expression[[1]]

# preparing Bayes Factor caption
if (type != "bayes" && isTRUE(bf.message) && isFALSE(paired)) {
caption_df <- tryCatch(rlang::exec(contingency_table, !!!.f.args, type = "bayes"),
error = function(e) NULL
)

caption_df <- eval_f(contingency_table, !!!.f.args, type = "bayes")
if (!is.null(caption_df)) caption <- caption_df$expression[[1]]
}
}
Expand Down
9 changes: 3 additions & 6 deletions R/ggscatterstats.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,13 @@ ggscatterstats <- function(data,
top.text = caption
)

subtitle_df <- rlang::exec(corr_test, !!!.f.args, type = type)

subtitle_df <- eval_f(corr_test, !!!.f.args, type = type)
subtitle <- if (!is.null(subtitle_df)) subtitle_df$expression[[1]]

# no need to use `tryCatch` because `correlation` already does this
# preparing the BF message for null hypothesis support
if (type == "parametric" && isTRUE(bf.message)) {
caption_df <- rlang::exec(corr_test, !!!.f.args, type = "bayes")

caption <- caption_df$expression[[1]]
caption_df <- eval_f(corr_test, !!!.f.args, type = "bayes")
caption <- if (!is.null(caption_df)) caption_df$expression[[1]]
}
}

Expand Down
12 changes: 2 additions & 10 deletions R/ggwithinstats.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,12 @@ ggwithinstats <- function(data,
)

.f <- function_switch(test)

subtitle_df <- tryCatch(
rlang::exec(.f, !!!.f.args, type = type),
error = function(e) NULL
)

subtitle_df <- eval_f(.f, !!!.f.args, type = type)
subtitle <- if (!is.null(subtitle_df)) subtitle_df$expression[[1]]

# preparing the Bayes factor message
if (type == "parametric" && isTRUE(bf.message)) {
caption_df <- tryCatch(rlang::exec(.f, !!!.f.args, type = "bayes"),
error = function(e) NULL
)

caption_df <- eval_f(.f, !!!.f.args, type = "bayes")
caption <- if (!is.null(caption_df)) caption_df$expression[[1]]
}
}
Expand Down

0 comments on commit 5299455

Please sign in to comment.