I'm trying to use the following glue
code to create an informative error message
library(rlang)
library(glue)
my_function <- function(x) {
UseMethod("my_function", x)
}
my_function.default <- function(x) {
abort(glue(
"Can't calculate my_function because { deparse(substitute(x)) } is of type ",
glue_collapse(class(x))
))
}
Using this test list we see it works:
test <- list(
x = c(1,2,3),
y = c("one", "two", "three")
)
my_function(test[[1]])
Error: Can't calculate my_function because test[[1]] is of type numeric
Run `rlang::last_error()` to see where the error occurred.
But is it possible to use glue
to have the the error return x
where it says test[[1]]
resulting in the error:
Can't calculate my_function because x is of type numeric