Skip to content

Commit

Permalink
DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 17, 2024
1 parent 8de01b4 commit 81fd432
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
11 changes: 2 additions & 9 deletions R/equivalence_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,9 @@ equivalence_test.data.frame <- function(x, range = "default", ci = 0.95, rvar_co

# multiple ranges for the parameters - iterate over parameters and range
if (is.list(range)) {
if (length(range) != ncol(x)) {
insight::format_error("Length of `range` (i.e. number of ROPE limits) should match the number of parameters.")
}
# check if list of values contains only valid values
checks <- vapply(range, function(r) {
!all(r == "default") || !all(is.numeric(r)) || length(r) != 2
}, logical(1))
if (!all(checks)) {
insight::format_error("`range` should be 'default' or a vector of 2 numeric values (e.g., c(-0.1, 0.1)).")
}
.check_list_range(range, x)
# apply thresholds to each column
l <- insight::compact_list(mapply(

Check warning on line 173 in R/equivalence_test.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/equivalence_test.R,line=173,col=32,[undesirable_function_linter] Avoid undesirable function "mapply".
function(p, r) {
equivalence_test(
Expand Down
28 changes: 19 additions & 9 deletions R/p_significance.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,9 @@ p_significance.data.frame <- function(x, threshold = "default", rvar_col = NULL,
if (ncol(x) == 1) {
ps <- .p_significance(x[, 1], threshold = threshold, ...)
} else if (is.list(threshold)) {
if (length(threshold) != ncol(x)) {
insight::format_error("Length of `threshold` should match the number of parameters.")
}
# check if list of values contains only valid values
checks <- vapply(threshold, function(r) {
!all(r == "default") || !all(is.numeric(r)) || length(r) > 2
}, logical(1))
if (!all(checks)) {
insight::format_error("`threshold` should be 'default' or a vector of 2 numeric values (e.g., c(-0.1, 0.1)).")
}
.check_list_range(threshold, x, larger_two = TRUE)
# apply thresholds to each column
ps <- mapply(

Check warning on line 143 in R/p_significance.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/p_significance.R,line=143,col=11,[undesirable_function_linter] Avoid undesirable function "mapply".
function(p, thres) {
.p_significance(
Expand Down Expand Up @@ -422,3 +415,20 @@ as.double.p_significance <- as.numeric.p_significance
}
threshold
}

.check_list_range <- function(range, params, larger_two = FALSE) {
if (length(range) != ncol(params)) {
insight::format_error("Length of `range` (i.e. number of ROPE limits) should match the number of parameters.")
}
# check if list of values contains only valid values
checks <- vapply(range, function(r) {
if (larger_two) {
!all(r == "default") || !all(is.numeric(r)) || length(r) > 2
} else {
!all(r == "default") || !all(is.numeric(r)) || length(r) != 2
}
}, logical(1))
if (!all(checks)) {
insight::format_error("`range` should be 'default' or a vector of 2 numeric values (e.g., c(-0.1, 0.1)).")
}
}
11 changes: 2 additions & 9 deletions R/rope.R
Original file line number Diff line number Diff line change
Expand Up @@ -612,16 +612,9 @@ rope.sim <- function(x, range = "default", ci = 0.95, ci_method = "ETI", paramet
#' @keywords internal
.prepare_rope_df <- function(parms, range, ci, ci_method, verbose) {
if (is.list(range)) {
if (length(range) != ncol(parms)) {
insight::format_error("Length of `range` (i.e. number of ROPE limits) should match the number of parameters.")
}
# check if list of values contains only valid values
checks <- vapply(range, function(r) {
!all(r == "default") || !all(is.numeric(r)) || length(r) != 2
}, logical(1))
if (!all(checks)) {
insight::format_error("`range` should be 'default' or a vector of 2 numeric values (e.g., c(-0.1, 0.1)).")
}
.check_list_range(range, params)
# apply thresholds to each column
tmp <- mapply(
function(p, r) {
rope(
Expand Down

0 comments on commit 81fd432

Please sign in to comment.