Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR] Allow showing expression instead of values in arg_match() error message #1686

Open
mikmart opened this issue Feb 8, 2024 · 0 comments

Comments

@mikmart
Copy link
Contributor

mikmart commented Feb 8, 2024

Sometimes I need to match an argument against a long (100+) list of valid values. Currently the arg_match() error message will be overwhelmed by listing out all possible valid values. In such cases, it would be useful to have a way to include the expression from the formal argument in the message instead.

For example with:

foo <- function(dataset = ls("package:datasets")) {
  rlang::arg_match(dataset)
}

foo("Titanci")
#> Error in `foo()`:
#> ! `dataset` must be one of "ability.cov", "airmiles", "AirPassengers",
#>   "airquality", "anscombe", "attenu", "attitude", "austres", "beaver1",
#>   "beaver2", "BJsales", "BJsales.lead", "BOD", "cars", "ChickWeight",
#>   "chickwts", "co2", "CO2", "crimtab", "discoveries", "DNase", "esoph", "euro",
#>   "euro.cross", "eurodist", "EuStockMarkets", "faithful", "fdeaths",
#>   "Formaldehyde", "freeny", "freeny.x", "freeny.y", "HairEyeColor",
#>   "Harman23.cor", "Harman74.cor", "Indometh", "infert", "InsectSprays", "iris",
#>   "iris3", "islands", "JohnsonJohnson", "LakeHuron", "ldeaths", "lh",
#>   "LifeCycleSavings", "Loblolly", "longley", "lynx", "mdeaths", "morley",
#>   "mtcars", "nhtemp", "Nile", "nottem", "npk", "occupationalStatus", "Orange",
#>   "OrchardSprays", "PlantGrowth", "precip", "presidents", "pressure",
#>   "Puromycin", "quakes", "randu", "rivers", "rock", "Seatbelts", "sleep",
#>   "stack.loss", "stack.x", "stackloss", "state.abb", "state.area",
#>   "state.center", "state.division", "state.name", "state.region", "state.x77",
#>   "sunspot.month", "sunspot.year", "sunspots", "swiss", "Theoph", "Titanic",
#>   "ToothGrowth", "treering", "trees", "UCBAdmissions", "UKDriverDeaths",
#>   "UKgas", "USAccDeaths", "USArrests", "UScitiesD", "USJudgeRatings",
#>   "USPersonalExpenditure", "uspop", "VADeaths", "volcano", "warpbreaks",
#>   "women", "WorldPhones", or "WWWusage", not "Titanci".
#> ℹ Did you mean "Titanic"?

It would be nice to be able to instead get:

foo("Titanci")
#> Error in `foo()`:
#> ! `dataset` must be one of `ls("package:datasets")`, not "Titanci".
#> ℹ Did you mean "Titanic"?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant