r-lib / rlang

Low-level API for programming with R
https://rlang.r-lib.org
Other
502 stars 139 forks source link

`arg_match()` error message only mentions the first element of `arg` #1682

Open ndunnewind opened 8 months ago

ndunnewind commented 8 months ago

If arg in arg_match() is a character vector with more than one element (and is not a permuted version of values) the error message only mentions the first element. This is confusing if the first element is actually in values, as shown below.

fn <- function(x = c("foo", "bar")) rlang::arg_match(x)

fn(c("foo", "baz"))
#> Error in `fn()`:
#> ! `x` must be one of "foo" or "bar", not "foo".

Created on 2024-01-22 with reprex v2.1.0

romainfrancois commented 6 months ago

It's also an issue when x is a vector and multiple is set to TRUE:

sorting_hat <- function(house = c("Gryffindor", "Slytherin", "Ravenclaw", "Hufflepuff"), multiple = FALSE) {
  rlang::arg_match(house, multiple = multiple)
}
sorting_hat(c("Gryffindor", "Slytherin"), multiple = TRUE)
#> [1] "Gryffindor" "Slytherin"
sorting_hat(c("Gryffindor", "Slytherin"), multiple = FALSE)
#> Error in `sorting_hat()`:
#> ! `house` must be one of "Gryffindor", "Slytherin", "Ravenclaw", or
#>   "Hufflepuff", not "Gryffindor".

Created on 2024-04-05 with reprex v2.1.0

olivroy commented 4 months ago

This is unrelated.