我正在尝试使用以下glue
代码来创建信息丰富的错误消息
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))
))
}
使用这个测试列表,我们看到它有效:
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.
但是是否可以使用在它说导致glue
错误的地方返回错误:x
test[[1]]
Can't calculate my_function because x is of type numeric