Dear R Users, what is the best way to trace the origin of warning messages? If an error occurs I can use traceback() to see where it comes from. I would like to do similar investigation, where a warning message originates from. Is there an option to turn warnings to errors? I tried the following> fWarn <- function(){ warning("A") } > options(warning.expression = stop()) > fWarn() > traceback()2: stop() 1: options(warning.expression = stop()) Hence, it does not give me the desired stack trace and the information that the warning occured in function fWarn, so far. (using Windows XP, R version 2.10.1) Best regards Thomas Wutzler
Use options(warn=2) Uwe Ligges On 03.05.2010 08:55, Thomas Wutzler wrote:> Dear R Users, > > what is the best way to trace the origin of warning messages? > > If an error occurs I can use traceback() to see where it comes from. I > would like to do similar investigation, where a warning message > originates from. > Is there an option to turn warnings to errors? > > I tried the following >> fWarn<- function(){ warning("A") } >> options(warning.expression = stop()) >> fWarn() >> traceback() > 2: stop() > 1: options(warning.expression = stop()) > > Hence, it does not give me the desired stack trace and the information > that the warning occured in function fWarn, so far. > > (using Windows XP, R version 2.10.1) > > Best regards > Thomas Wutzler > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On Mon, 3 May 2010, Thomas Wutzler wrote:> Dear R Users, > > what is the best way to trace the origin of warning messages?options(warn=2) at least for the first warning.> If an error occurs I can use traceback() to see where it comes from. I > would like to do similar investigation, where a warning message > originates from. > Is there an option to turn warnings to errors?Yes: did you not find it under ?options ?> I tried the following >> fWarn <- function(){ warning("A") } >> options(warning.expression = stop()) >> fWarn() >> traceback() > 2: stop() > 1: options(warning.expression = stop()) > > Hence, it does not give me the desired stack trace and the information > that the warning occured in function fWarn, so far.So print something useful there like the call stack, see ?sys.call.> (using Windows XP, R version 2.10.1) > > Best regards > Thomas Wutzler-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Thanks Mr. Ripley, Mr. Liggers, and Mr. Burns, "I can not see the forest, because trees block my sight" options(warn=2) Thanks also for the hint to the very useful R Inferno. I guess, reading it earlier, would have saved days of debugging and frustration. I also did not now about the useful browser, recover, and trace functions yet. Just of interest: Is there a way to trace the nth warning, i.e. continue on the first n-1 warnings and stop only afterwards? Best regards Thomas Wutzler Thomas Wutzler wrote:> Dear R Users, > > what is the best way to trace the origin of warning messages? > > If an error occurs I can use traceback() to see where it comes from. I > would like to do similar investigation, where a warning message > originates from. > Is there an option to turn warnings to errors? > > I tried the following >> fWarn <- function(){ warning("A") } >> options(warning.expression = stop()) >> fWarn() >> traceback() > 2: stop() > 1: options(warning.expression = stop()) > > Hence, it does not give me the desired stack trace and the information > that the warning occured in function fWarn, so far. > > (using Windows XP, R version 2.10.1) > > Best regards > Thomas Wutzler