[英]How to get a more informative assertion when using assertr::verify in R?
meses <- c("A", "B", "C")
tibble::tibble(
mes = c("A", "b", "C")
) %>%
assertr::verify(mes %in% meses)
来自verify
的消息。 这是正确的,因为它说的是错误的索引,但是在value
列中没有显示tibble
中mes
列的值,所以我必须查找该行。
verb redux_fn predicate column index value
1 verify NA mes %in% meses NA 2 NA
期望的输出
verb redux_fn predicate column index value
1 verify NA mes %in% meses NA 2 b
我们可以使用assert
来返回“值”
library(assertr)
library(tibble)
tibble(mes = c("A", "b", "C")) %>%
assert(function(x) x %in% meses, mes)
Column 'mes' violates assertion 'function(x) x %in% meses' 1 time
verb redux_fn predicate column index value
1 assert NA function(x) x %in% meses mes 2 b
使用verify
时, error_fun
中的默认选项是error_stop
。 如果我们可以更改默认选项,它应该可以工作
tibble::tibble(
mes = c("A", "b", "C")
) %>%
assertr::verify(mes %in% meses,
error_fun = function(errors, data = .)
{
errors[[1]]$error_df$value <- .$mes[errors[[1]]$error_df$index]
errors[[1]]
})
-输出
verification [mes %in% meses] failed! (1 failure)
verb redux_fn predicate column index value
1 verify NA mes %in% meses NA 2 b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.