Skip to content

Commit dc7de24

Browse files
committed
replace with validate_argument
1 parent 3738931 commit dc7de24

File tree

8 files changed

+19
-89
lines changed

8 files changed

+19
-89
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,4 @@ Config/testthat/edition: 3
223223
Config/testthat/parallel: true
224224
Config/Needs/website: easystats/easystatstemplate
225225
Config/rcmdcheck/ignore-inconsequential-notes: true
226+
Remotes: easystats/insight#938

R/2_ci.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ ci.glm <- function(x,
7373
vcov_args = NULL,
7474
verbose = TRUE,
7575
...) {
76-
method <- .check_arg(method, c("profile", "wald", "normal", "residual"))
76+
method <- insight::validate_argument(method, c("profile", "wald", "normal", "residual"))
7777

7878
# No robust vcov for profile method
7979
if (method == "profile") {

R/bootstrap_model.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ bootstrap_model.default <- function(model,
7575

7676
insight::check_if_installed("boot")
7777

78-
type <- .check_arg(type, c("ordinary", "parametric", "balanced", "permutation", "antithetic"))
78+
type <- insight::validate_argument(type, c("ordinary", "parametric", "balanced", "permutation", "antithetic"))
7979
parallel <- match.arg(parallel)
8080

8181
model_data <- data <- insight::get_data(model, verbose = FALSE) # nolint
@@ -156,7 +156,7 @@ bootstrap_model.merMod <- function(model,
156156
...) {
157157
insight::check_if_installed("lme4")
158158

159-
type <- .check_arg(type, c("parametric", "semiparametric"))
159+
type <- insight::validate_argument(type, c("parametric", "semiparametric"))
160160
parallel <- match.arg(parallel)
161161

162162
boot_function <- function(model) {
@@ -228,7 +228,7 @@ bootstrap_model.nestedLogit <- function(model,
228228
...) {
229229
insight::check_if_installed("boot")
230230

231-
type <- .check_arg(type, c("ordinary", "balanced", "permutation", "antithetic"))
231+
type <- insight::validate_argument(type, c("ordinary", "balanced", "permutation", "antithetic"))
232232
parallel <- match.arg(parallel)
233233

234234
model_data <- data <- insight::get_data(model, verbose = FALSE) # nolint

R/ci_generic.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
method <- "wald"
1919
}
2020
method <- tolower(method)
21-
method <- .check_arg(
21+
method <- insight::validate_argument(
2222
method,
2323
c("wald", "ml1", "betwithin", "kr", "satterthwaite", "kenward", "boot",
2424
"profile", "residual", "normal"

R/extract_random_variances.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
ci_random = NULL,
4646
verbose = FALSE,
4747
...) {
48-
component <- .check_arg(component, c("all", "conditional", "zero_inflated", "zi", "dispersion"))
48+
component <- insight::validate_argument(component, c("all", "conditional", "zero_inflated", "zi", "dispersion"))
4949

5050
out <- suppressWarnings(
5151
.extract_random_variances_helper(

R/methods_glmmTMB.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ model_parameters.glmmTMB <- function(model,
4747
ci_method <- .check_df_method(ci_method)
4848

4949
# which components to return?
50-
effects <- .check_arg(effects, c("fixed", "random", "all"))
51-
component <- .check_arg(component, c("all", "conditional", "zi", "zero_inflated", "dispersion"))
50+
effects <- insight::validate_argument(effects, c("fixed", "random", "all"))
51+
component <- insight::validate_argument(component, c("all", "conditional", "zi", "zero_inflated", "dispersion"))
5252

5353
# standardize only works for fixed effects...
5454
if (!is.null(standardize) && standardize != "refit") {
@@ -268,8 +268,8 @@ ci.glmmTMB <- function(x,
268268
verbose = TRUE,
269269
...) {
270270
method <- tolower(method)
271-
method <- .check_arg(method, c("wald", "normal", "ml1", "betwithin", "profile", "uniroot", "robust"))
272-
component <- .check_arg(component, c("all", "conditional", "zi", "zero_inflated", "dispersion"))
271+
method <- insight::validate_argument(method, c("wald", "normal", "ml1", "betwithin", "profile", "uniroot", "robust"))
272+
component <- insight::validate_argument(component, c("all", "conditional", "zi", "zero_inflated", "dispersion"))
273273

274274
if (is.null(.check_component(x, component, verbose = verbose))) {
275275
return(NULL)
@@ -315,8 +315,8 @@ standard_error.glmmTMB <- function(model,
315315
component = "all",
316316
verbose = TRUE,
317317
...) {
318-
component <- .check_arg(component, c("all", "conditional", "zi", "zero_inflated", "dispersion"))
319-
effects <- .check_arg(effects, c("fixed", "random"))
318+
component <- insight::validate_argument(component, c("all", "conditional", "zi", "zero_inflated", "dispersion"))
319+
effects <- insight::validate_argument(effects, c("fixed", "random"))
320320

321321
dot_args <- .check_dots(
322322
dots = list(...),
@@ -377,7 +377,7 @@ simulate_model.glmmTMB <- function(model,
377377
component = "all",
378378
verbose = FALSE,
379379
...) {
380-
component <- .check_arg(component, c("all", "conditional", "zi", "zero_inflated", "dispersion"))
380+
component <- insight::validate_argument(component, c("all", "conditional", "zi", "zero_inflated", "dispersion"))
381381
info <- insight::model_info(model, verbose = FALSE)
382382

383383
## TODO remove is.list() when insight 0.8.3 on CRAN

R/methods_lme4.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,12 @@ model_parameters.merMod <- function(model,
193193
ci_method <- tolower(ci_method)
194194

195195
if (isTRUE(bootstrap)) {
196-
ci_method <- .check_arg(
196+
ci_method <- insight::validate_argument(
197197
ci_method,
198198
c("hdi", "quantile", "ci", "eti", "si", "bci", "bcai")
199199
)
200200
} else {
201-
ci_method <- .check_arg(
201+
ci_method <- insight::validate_argument(
202202
ci_method,
203203
c(
204204
"wald", "normal", "residual", "ml1", "betwithin", "satterthwaite",
@@ -208,7 +208,7 @@ model_parameters.merMod <- function(model,
208208
}
209209

210210
# which component to return?
211-
effects <- .check_arg(effects, c("fixed", "random", "all"))
211+
effects <- insight::validate_argument(effects, c("fixed", "random", "all"))
212212
params <- params_random <- params_variance <- NULL
213213

214214
# post hoc standardize only works for fixed effects...
@@ -343,7 +343,7 @@ ci.merMod <- function(x,
343343
iterations = 500,
344344
...) {
345345
method <- tolower(method)
346-
method <- .check_arg(method, c(
346+
method <- insight::validate_argument(method, c(
347347
"wald", "ml1", "betwithin", "kr",
348348
"satterthwaite", "kenward", "boot",
349349
"profile", "residual", "normal"
@@ -379,7 +379,7 @@ standard_error.merMod <- function(model,
379379
vcov_args = NULL,
380380
...) {
381381
dots <- list(...)
382-
effects <- .check_arg(effects, c("fixed", "random"))
382+
effects <- insight::validate_argument(effects, c("fixed", "random"))
383383

384384
if (effects == "random") {
385385
out <- .standard_errors_random(model)

R/utils.R

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -231,74 +231,3 @@
231231
))
232232
}
233233
}
234-
235-
236-
# this is a wrapper around `match.arg()`, but provided clearer information on fail
237-
.check_arg <- function(argument, options) {
238-
argument_name <- deparse(substitute(argument))
239-
argument <- .safe(match.arg(argument, options))
240-
if (is.null(argument)) {
241-
suggestion <- .misspelled_string(options, argument_name)
242-
msg <- sprintf("Invalid option for argument `%s`.", argument_name)
243-
if (is.null(suggestion) || !length(suggestion) || !nzchar(suggestion)) {
244-
msg <- paste(msg, "Please use one of the following options:")
245-
} else {
246-
msg <- paste(msg, suggestion, "Otherwise, use one of the following options:")
247-
}
248-
msg <- paste(msg, datawizard::text_concatenate(options, last = " or ", enclose = "\""))
249-
insight::format_error(msg)
250-
}
251-
argument
252-
}
253-
254-
255-
.misspelled_string <- function(source, searchterm, default_message = NULL) {
256-
if (is.null(searchterm) || length(searchterm) < 1) {
257-
return(default_message)
258-
}
259-
# used for many matches
260-
more_found <- ""
261-
# init default
262-
msg <- ""
263-
# remove matching strings
264-
same <- intersect(source, searchterm)
265-
searchterm <- setdiff(searchterm, same)
266-
source <- setdiff(source, same)
267-
# guess the misspelled string
268-
possible_strings <- unlist(lapply(searchterm, function(s) {
269-
source[.fuzzy_grep(source, s)] # nolint
270-
}), use.names = FALSE)
271-
if (length(possible_strings)) {
272-
msg <- "Did you mean "
273-
if (length(possible_strings) > 1) {
274-
# make sure we don't print dozens of alternatives for larger data frames
275-
if (length(possible_strings) > 5) {
276-
more_found <- sprintf(
277-
" We even found %i more possible matches, not shown here.",
278-
length(possible_strings) - 5
279-
)
280-
possible_strings <- possible_strings[1:5]
281-
}
282-
msg <- paste0(msg, "one of ", datawizard::text_concatenate(possible_strings, last = " or ", enclose = "\""))
283-
} else {
284-
msg <- paste0(msg, "\"", possible_strings, "\"")
285-
}
286-
msg <- paste0(msg, "?", more_found)
287-
} else {
288-
msg <- default_message
289-
}
290-
# no double white space
291-
insight::trim_ws(msg)
292-
}
293-
294-
295-
.fuzzy_grep <- function (x, pattern, precision = NULL) {
296-
if (is.null(precision)) {
297-
precision <- round(nchar(pattern) / 3)
298-
}
299-
if (precision > nchar(pattern)) {
300-
return(NULL)
301-
}
302-
p <- sprintf("(%s){~%i}", pattern, precision)
303-
grep(pattern = p, x = x, ignore.case = FALSE)
304-
}

0 commit comments

Comments
 (0)