Skip to content
Prev 31270 / 63424 Next

Capturing all warnings (with messages)

Jeffrey Horner wrote on 02/04/2009 01:19 PM:
I'm going to take a stab in the dark and presume you want to trap 
warnings  so that they aren't displayed. Using options(warn=-1) and the 
following could trap warnings allowing you to examine them and continue 
with code execution:

 > options('warn')
$warn
[1] -1

 > system('cat /tmp/foo.R')
warning("A simple warning")
cat("continuing...\n")
 > withCallingHandlers(source('/tmp/foo.R'), warning = function(e) str(e))
List of 2
  $ message: chr "A simple warning"
  $ call   : language eval.with.vis(expr, envir, enclos)
  - attr(*, "class")= chr [1:3] "simpleWarning" "warning" "condition"
continuing...