Skip to content

keep information on the number of warnings

4 messages · Luis Borda de Agua, David Winsemius, William Dunlap

#
On Aug 4, 2014, at 9:24 AM, Luis Borda de Agua wrote:

            
I have just disproven my initial hypothesis that I formed upon reading the R code of `warnings` that it had to do with the environment in which the warning was generated:
Warning messages:
1: In g() : inner warn
2: In f() : outer warn
Warning messages:
1: In g() : inner warn
2: In f() : outer warn

So, I simply don't know (and you have provided no example to reproduce the issue). May I remind you to include context from earlier messages? Looking back at your first message the number 36 appears rather than 38. Perhaps the number of warning varies from run to run? You would wnat to compare the number from such a message to the output of length(warnings()) after the run rather than before it.

---------
--------------------

David.
David Winsemius
Alameda, CA, USA
#
Look at withCallngHandlers for another way to capture warnings.  It
will let you attach warnings from an iteration of your function to the
output of the function so you can later track down the root cause of
the warning.

E.g., the attached captureWarningsAndMessagesWithContext attaches
warnings, messages, and errors to the output, along with a traceback
for each.

captureWarningsAndMessagesWithContext <-
function(expr) {

   warnings <- list()
   messages <- list()
   fmt <- function(cond, sysCalls) {
      c(Message = conditionMessage(cond),
        Call = deparse(conditionCall(cond))[1],
        rev(vapply(sysCalls, function(sc)deparse(sc)[1], "")))
   }
   retval <- list(result=withCallingHandlers(try(expr, silent=TRUE),
                                  warning=function(w){
                                      warnings[[length(warnings)+1]]
<<- fmt(w, sys.calls())
                                      invokeRestart("muffleWarning")
                                  },
                                  message=function(m){
                                      messages[[length(messages)+1]]
<<- fmt(m, sys.calls())
                                      invokeRestart("muffleMessage")
                                  }
                                 )
                  )
   # retval$result will have class "try-error" if there was an error,
   # which the caller can check for.
   retval$messages <- messages
   retval$warnings <- warnings
   retval
}

E.g.,

z <- lapply(list(log, lm, function(x)1/x),
function(fun)captureWarningsAndMessagesWithContext(fun(-1)))

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Mon, Aug 4, 2014 at 9:24 AM, Luis Borda de Agua
<lbagua.cloud at gmail.com> wrote:
1 day later