Print warning messages and save them automatically in a file

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Print warning messages and save them automatically in a file

Alexander
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
Reply | Threaded
Open this post in threaded view
|

Re: Print warning messages and save them automatically in a file

Alexander
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
Reply | Threaded
Open this post in threaded view
|

Re: Print warning messages and save them automatically in a file

glsnow
In reply to this post by Alexander
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 <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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.
[hidden email]

______________________________________________
[hidden email] 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.