[R] Print warning messages and save them automatically in a file

Alexander juschitz_alexander at yahoo.de
Thu Apr 19 10:00:55 CEST 2012


Hello,
I am working under R2.11.0 Windows and I would like to ask you if you know a
way to save all warning messages obtained by the R function "warning" in a
file and keeping the functionalities of the base-function warning. For
example if I use external code, I don't want to replace all lines containing
"warning(...)" by a selfwritten function. I want to execute it normally and
everytime the external code makes a call to warning, I want the warnings
message printed out in the console AND written in a file.

My first solution is to redefine the function warning in the global
environment such as:

warning <- function(...){
   write(...,"Warning.log",append=TRUE)
   base::warning(...) 
#unfortunately the warning happens always in the function warning of the
.GlobalEnv
#and doesn't indicate anymore where the error happens :-(
}

This solution isn't very clean. I would like to try to redefine
"warning.expression" in options. In last case, I don't understand how the
passing of arguments works. I would like to do something like:

options(warning.expression=quote({
  write(...,"Warning.log",append=TRUE)
  ?????
 }))

I put the ???? because I don't know how I should call the function warning
without being recursive and how I can pas arguments.

Thank you

Alexander

--
View this message in context: http://r.789695.n4.nabble.com/Print-warning-messages-and-save-them-automatically-in-a-file-tp4570163p4570163.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list