Re: [R] Appending diagnostic information to all lines sent to stdout and stderr

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Thu, 19 Jun 2008 08:26:20 +0100 (BST)

> Is this actually possible?

Yes. All R output to stdout/stderr that will get redirected by sink() passes through a connection. You can modify the sources to make a connection which adds info. Whether the information you want is available at that point is another matter -- and for things like the date you could do this by using a pipe() connections and writing a suitable filter.

On Thu, 19 Jun 2008, Thomas Allen wrote:

> Dear All
>
> I'm logging the stdout and stderr of an R program into two separate
> files (stderr.txt and stdout.txt) using sink()
> I would like to append extra information such as "date", "memory
> usage" etc to every line of output that goes to stdout or stderr.
>
> For example
>
>> cat("hello \n")
>
> should give output that looks something like:
> "hello --- Thu Jun 19 18:39:15 2008 | cel_norm_expm | 389.6 Mb used |"
> instead of just:
> "hello"
>
> I don't want to replace functions like "print", "cat", "stop" and
> "warning" with my own special functions.
> Instead, I want to somehow catch the messages that these functions
> return and alter them before writing to my
> logging files.
> Any code that writes output by making calls to cat etc should be
> completely isolated from the mechanism that is adding this extra
> diagnostic information.
>
> Is this actually possible?
> I don't really like the idea of rewriting the "cat" function and
> creating output for stdout from stderr using "kludges" like:
>
>> options(error=function(x=geterrmessage()){cat(x)})
>
> Thank you for any help and please ask me to explain my situation in
> more detail if necessary
>
> --
> Thomas Allen
> Department of Biochemistry
> University of Otago
> 710 Cumberland street
> Box 56
> Dunedin
> New Zealand
> email1: hedbag_at_gmail.com
> email2: allth025_at_student.otago.ac.nz
>
> Only the insane have strength enough to prosper and only those who
> prosper can truly judge what is sane
>
> ______________________________________________
> 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.
>

-- 
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

______________________________________________
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.
Received on Thu 19 Jun 2008 - 08:23:03 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 19 Jun 2008 - 09:31:03 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive