mllg / checkmate

Fast and versatile argument checks
https://mllg.github.io/checkmate/
Other
261 stars 30 forks source link

Original variable name does not appear when `matchArg` doesn't find choice #251

Open averissimo opened 9 months ago

averissimo commented 9 months ago

The assertion message doesn't show the original variable name

image

See reproducible example below (session info at the end):

foo <- function(arg1 = c("a", "b", "c")) {
  arg1 <- checkmate::matchArg(arg1, choices = c("a", "b", "c"))
  # ...
}

# Manual workaround
foo2 <- function(arg1 = c("a", "b", "c")) {
  arg1 <- checkmate::matchArg(arg1, choices = c("a", "b", "c"), .var.name = "arg1")
  # ...
}

# Works great
foo("a")

# 'character(0)' instead of 'arg1'
foo("d")
#> Error in checkmate::matchArg(arg1, choices = c("a", "b", "c")): Assertion on 'character(0)' failed: Must be element of set {'a','b','c'}, but is not atomic scalar.

# Work around
foo2("d")
#> Error in checkmate::matchArg(arg1, choices = c("a", "b", "c"), .var.name = "arg1"): Assertion on 'arg1' failed: Must be element of set {'a','b','c'}, but is not atomic scalar.
Session info ```r R version 4.3.2 (2023-10-31) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 23.10 Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.11.0 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.11.0 locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_IE.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_IE.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_IE.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_IE.UTF-8 LC_IDENTIFICATION=C time zone: Europe/Berlin tzcode source: system (glibc) attached base packages: [1] stats graphics grDevices datasets utils methods base other attached packages: [1] teal.data_0.3.0.9019 teal.code_0.4.1.9015 testthat_3.2.0 loaded via a namespace (and not attached): [1] R.utils_2.12.2 lazyeval_0.2.2 fastmap_1.1.1 promises_1.2.1 reprex_2.0.2 rex_1.2.1 [7] digest_0.6.33 mime_0.12 lifecycle_1.0.4 waldo_0.5.1 ellipsis_0.3.2 processx_3.8.2 [13] magrittr_2.0.3 compiler_4.3.2 rlang_1.1.2 tools_4.3.2 utf8_1.2.3 yaml_2.3.7 [19] data.table_1.14.8 knitr_1.45 prettyunits_1.1.1 htmlwidgets_1.6.2 pkgbuild_1.4.2 xmlparsedata_1.0.5 [25] xml2_1.3.3 pkgload_1.3.2.1 R.cache_0.16.0 miniUI_0.1.1.1 withr_2.5.2 purrr_1.0.2 [31] R.oo_1.25.0 desc_1.4.2 fansi_1.0.4 diffobj_0.3.5 urlchecker_1.0.1 profvis_0.3.8 [37] xtable_1.8-4 cli_3.6.1 rmarkdown_2.25 crayon_1.5.2 remotes_2.4.2.1 rstudioapi_0.15.0 [43] sessioninfo_1.2.2 cachem_1.0.8 stringr_1.5.0 BiocManager_1.30.22 vctrs_0.6.4 devtools_2.4.5 [49] callr_3.7.3 cyclocomp_1.1.1 clipr_0.8.0 glue_1.6.2 rematch2_2.1.2 ps_1.7.5 [55] stringi_1.7.12 later_1.3.1 tibble_3.2.1 styler_1.10.2 pillar_1.9.0 htmltools_0.5.7 [61] brio_1.1.3 R6_2.5.1 lintr_3.1.1 rprojroot_2.0.3 evaluate_0.21 shiny_1.8.0 [67] R.methodsS3_1.8.2 backports_1.4.1 memoise_2.0.1 renv_1.0.2 httpuv_1.6.11.9000 Rcpp_1.0.11 [73] checkmate_2.3.1 xfun_0.40 fs_1.6.3 usethis_2.2.1 pkgconfig_2.0.3 ```