我正在使用 stopifnot
我知道它只返回第一个不是 TRUE
的值.如果这是一些奇怪的动态表达式,那么不熟悉自定义函数的人无法真正从中做出一些贡献。所以我很想添加一个自定义错误消息。有什么建议?
Error: length(unique(nchar(check))) == 1 is not TRUE
基本上说明向量的元素
check
没有相同的长度。有没有办法说:
Error: Elements of your input vector do not have the same length!
?
最佳答案
使用 stop
和一个 if
陈述:
if(length(unique(nchar(check))) != 1)
stop("Error: Elements of your input vector do not have the same length!")
只要记住
stopifnot
有陈述否定的方便,所以你的条件在if
需要是您停止条件的否定。这是错误消息的样子:
> check = c("x", "xx", "xxx")
> if(length(unique(nchar(check))) != 1)
+ stop("Error: Elements of your input vector do not have the same length!")
Error in eval(expr, envir, enclos) :
Error: Elements of your input vector do not have the same length!
关于r - stopifnot 更好的错误信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8343509/