Hi all,
I have a function that takes a vector. Under some conditions (i.e. an invalid vector), I want to throw an error (stop("...")
).
I want to raise an error message that contains a copy-paste-friendly representation of the vector that was input. Ideally something like:
my_invalid_input <- c("a", 1, "c")
my_function(my_invalid_input)
#> Error in my_function(my_invalid_input): Invalid subset c("a", 1, "c")
I can get close with something like
my_function <- function(x) {
...
if (is_invalid(x)) {
x_str <- glue::collapse(x, ", ")
stop(glue::glue("Invalid subset c({x_str})"))
...
}
#> Error in my_function(my_invalid_input): Invalid subset c(a, 1, c)
But this still isn't pasteable (lack of quotation marks).
My questions boil down to:
- Is this a worthwhile idea? Or are there better practices for error messages in R (if so - where can I find out more about them)?
- Is this possible? And how would I go about doing this?
In the latter case, it seems like rlang
or lazyeval
might be useful?