[英]In R, how to know if a function printed warning messages to the console before return?
func <- function() {
warning("warning message")
return(1)
}
func
will print a warning before returning. func
将在返回前打印一条警告。 But how can the fact that it print warning be detected by code?character
value?character
值? You can wrap a call to the function with tryCatch
to catch warnings.您可以使用
tryCatch
包装对 function 的调用以捕获警告。 For example you can do it around the function itself例如,您可以围绕 function 本身进行操作
x <- tryCatch(func(), warning= function(w) {
message(paste("a warning did occur:", conditionMessage(w)))
return(0)
})
x
or you could do it inside the function或者你可以在 function 里面做
func <- function() {
tryCatch({
warning("warning message")
}, warning = function(w) {
message(paste("a warning did occur:", conditionMessage(w)))
})
return(1)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.