您可以使用withCallingHandlers
来实时捕获(和 suppress )消息,然后使用unique
来减少它们,然后重新将它们释放出来.为了透明起见,重复使用时我会加上(n times)
(否则您可能不清楚内部调用有多吵).
functionC <- function(a,b){
msgs <- character(0)
a <- withCallingHandlers(
functionA(a),
message = function(m) {
msgs <<- c(msgs, conditionMessage(m))
invokeRestart("muffleMessage")
})
b <- withCallingHandlers(
functionB(b),
message = function(m) {
msgs <<- c(msgs, conditionMessage(m))
invokeRestart("muffleMessage")
})
msgs <- trimws(msgs)
# since 'table' does not preserve the original order, we'll do a few
# extra steps to ensure the messages appear in the order of their
# _first_ appearance
counts <- table(msgs)
counts <- counts[match(names(counts), msgs)]
msgs <- paste0(names(counts), ifelse(counts > 1, sprintf(" (%d times)", counts), ""))
for (m in msgs) message(m)
c <- rbind(a, b)
return(c)
}
functionC(5,5)
# The value is 5. (2 times)
# [,1]
# a 3
# b 12
您可以重复message=
/"muffleMessage"
和warning=
/"muffleWarning"
来捕捉警告...不过,您也可以使用purrr::quietly
:
functionC2 <- function(a,b){
msgs <- character(0)
warns <- character(0)
aout <- purrr::quietly(functionA)(a)
bout <- purrr::quietly(functionB)(b)
fewer <- function(z) {
z <- trimws(z)
counts <- table(z)
counts <- counts[match(names(counts), z)]
paste0(names(counts), ifelse(counts > 1, sprintf(" (%d times)", counts), ""))
}
msgs <- fewer(c(aout$messages, bout$messages))
warns <- fewer(c(aout$warnings, bout$warnings))
cout <- rbind(aout$result, bout$result)
for (m in msgs) message(m)
for (w in warns) warning(w)
return(cout)
}