Alexander
2012-Apr-19 08:00 UTC
[R] Print warning messages and save them automatically in a file
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.
Alexander
2012-Apr-19 15:06 UTC
[R] Print warning messages and save them automatically in a file
My temporary solution to the problem is "trace": trace(warning,tracer=quote({ tmp<-try(cat(paste(...),"warning.log",append=TRUE,fill=TRUE)) if(is(tmp,"try-error")){ print(sys.call()) print(unlist(...)) } })) with best regards Alexander wrote> > 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-tp4570163p4571173.html Sent from the R help mailing list archive at Nabble.com.
Greg Snow
2012-Apr-20 17:07 UTC
[R] Print warning messages and save them automatically in a file
Would using the 'sink' function with type='message' and split=TRUE do what you want? On Thu, Apr 19, 2012 at 2:00 AM, Alexander <juschitz_alexander at yahoo.de> wrote:> 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. > > ______________________________________________ > 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.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com