checkmate
checkmate copied to clipboard
Fixes `matchArg` var name problem and retrieves `choices` from formals when missing
Closes #251
Changes description
- Asserts
.var.namewhich corrects #251 - Better replicates
match.argfunctionality by inferring choices when called from a function- See new test
- Improve on message when
xis not inchoices
Example (when on PR branch)
🔎 note double problem in error message:
Assertion on character(0)...in the error message... but is not atomic scalar.in the error message
f1 <- function(x = c("aa", "bb", "cc")) {
checkmate::matchArg(x, choices = eval(formals(f1)[["x"]]))
}
f1("dd")
#> Error in checkmate::matchArg(x, choices = eval(formals(f1)[["x"]])):
#> Assertion on 'character(0)' failed: Must be element of set {'aa','bb','cc'}, but is not atomic scalar.
checkmate::matchArg("xx", choices = c("pearson", "kendall", "spearman"))
#> Error in checkmate::matchArg("xx", choices = c("pearson", "kendall", "spearman")):
#> Assertion on 'character(0)' failed: Must be element of set {'pearson','kendall','spearman'}, but is not atomic scalar.
🟢 Corrected
pkgload::load_all("/path/to/checkmate/with/branch/from/pr")
#> ℹ Loading checkmate
f1("dd")
#> Error in f1("dd"):
#> Assertion on 'x' failed: Must be element of set {'aa','bb','cc'}, but is 'dd'.
checkmate::matchArg("xx", choices = c("pearson", "kendall", "spearman"))
#> Error in eval(expr, envir, enclos):
#> Assertion on '"xx"' failed: Must be element of set {'pearson','kendall','spearman'}, but is 'xx'.
🟢 With missing choices
f2 <- function(x = c("aa", "bb", "cc")) {
res <- checkmate::matchArg(x)
res
}
f2("dd")
#> Error in f2("dd"):
#> Assertion on 'x' failed: Must be element of set {'aa','bb','cc'}, but is 'dd'.
f2("aa")
#> [1] "aa"
Created on 2024-02-28 with reprex v2.0.2