Better replicates match.arg functionality by inferring choices when called from a function
See new test
Improve on message when x is not in choices
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"
Closes #251
Changes description
.var.name
which corrects #251match.arg
functionality by inferring choices when called from a functionx
is 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š¢ Corrected
š¢ With missing choices
Created on 2024-02-28 with reprex v2.0.2