[R] How to catch both warnings and errors?

Marius Hofert m_hofert at web.de
Mon Dec 6 11:10:17 CET 2010


Hmm... still not quite what I was hoping for... but thanks anyway. 

I would like to have the output in the following form:

> f(1)
$result
[1] 0

$warning
[1] NULL # or ""

$error
[1] NULL # or ""

> f(-1)
$result
[1] NaN

$warning
[1] "Warning in log(-1) : NaNs produced" # or something similar

$error
[1] NULL # or ""

> f("a")
$result
[1] NULL # or NA 

$warning
[1] NULL # or ""

$error
[1] "Error in log("a") : Non-numeric argument to mathematical function"

Has anyone done that before? I think it's quite a natural problem although I could not find a solution online (dealing with *both* warnings *and* errors). 

Cheers,

Marius 



More information about the R-help mailing list