您可使用<代码>与CallingHandlers实时传送(和压制)电文,然后使用<代码>unique予以减少,然后将<>message予以删除。 为透明起见(否则,你可能不会清楚地知道如何进行内部调动),请加上<条码>(时段)。
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"
with press=
>>>>>>>----“muffle Warning”------------------------------------------------------------
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)
}