R/catchWarning.R

Defines functions catchWarning

Documented in catchWarning

#' Return Warning Message in Attribute
#' 
#' This function evaluates an expression and returns, if warnings occurred, the
#' warning message in the attribute "warningMessage" of the returned object.
#' 
#' @param expr expression to be evaluated
#' @param dbg if \code{TRUE} (the default) the warning text is also printed on
#'   the console, otherwise the warning message is suppressed.
#' @export
#' @examples 
#' catchWarning(as.numeric("1.23"))
#' result <- catchWarning(as.numeric("x"))
#' result <- catchWarning(as.numeric("x"), dbg = FALSE)
#' str(result)
catchWarning <- function(expr, dbg = TRUE)
{
  suppressWarnings(result <- tryCatch(
    eval(expr),  
    warning = function(w) {
      kwb.utils::printIf(dbg, w$message, caption = "There was a warning")
      structure(eval(expr), warningMessage = w$message)
    }
  ))
  
  result
}
KWB-R/kwb.utils documentation built on April 1, 2024, 7:12 a.m.