[R] Redirecting Messages and Errors to a file

Berton Gunter gunter.berton at gene.com
Wed Jul 27 18:00:32 CEST 2005


I agree that the Help file does not clearly state that one sink file can be
used, but neither does it say that it can't. So why not **try it and see**.
Following the Help instructions I tried:

> con<-file('test',open='w')
> sink(con)
## Now produce some output
> rnorm(10)
> sink(con,type='message',append=TRUE)
## produce an error message
> rnorm('abc')
## more output
> rnorm(10)
## stop sinking for messages and output
> sink(NULL,type='message')
> sink(NULL)
## see what went into the file
> readLines('test')

So one sees that one file **can** be used. One also reads that it probably
should **not** be.

While one would prefer that Help documents were written so clearly and
completely that no possibility of misinterpretation or confusion could
occur, that is a standard way beyond any to which I would wish to be held. I
think the core code documentation is generally remarkably clear (especially
when supplemented by the several available manuals), but sometimes
ambiguities like this do occur.  I have found that whenever I have questions
of this sort, a little experimentation like the above almost always resolves
my confusion -- and very rarely even uncovers a legitimate bug or
documentation error. I know this takes a bit more time than a post to the
list (which gracious gurus often answer in minutes), but you tend to learn a
lot more by doing the experimentation first.

-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
"The business of the statistician is to catalyze the scientific learning
process."  - George E. P. Box

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of McGehee, Robert
> Sent: Wednesday, July 27, 2005 7:55 AM
> To: Thomas Lumley
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] Redirecting Messages and Errors to a file
> Ahh, I misunderstood the help page. I took that since the 
> default values
> for type were c("output", "message"), that both the output _and_ the
> messages were simultaneously being redirected (else why not just make
> the default "output"). I see that this is not the case (as 
> noted in the
> details). 
> After specifying type = "message", I was also able to 
> successfully send
> off the error messages to a file, but not at the same time as sending
> the output. That is, do I need to have two sink files, one for errors,
> one for output and then append them before mailing? I'd 
> prefer a way of
> sending all output/messages to the same file to preserve the order of
> the messages/output, if this is possible.
> Robert
> -----Original Message-----
> From: Thomas Lumley [mailto:tlumley at u.washington.edu] 
> Sent: Wednesday, July 27, 2005 10:37 AM
> To: McGehee, Robert
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] Redirecting Messages and Errors to a file
> On Wed, 27 Jul 2005, McGehee, Robert wrote:
> >
> > However, the sink() does not seem to be capturing the error messages
> as
> > I would have hoped. That is, if the R-script is {print("abc");
> > stop("def")}, the print output is captured to the file, the 
> stop error
> > message is not, and instead sent to the screen.
> >
> > Any thoughts on how to do this such that everything is sent to the
> file
> > for emailing? Perhaps something different all together?
> There is a separate output connection for errors.  The help page for 
> sink() describes how to divert it, and warns of risks in doing so.
>  	-thomas
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html

More information about the R-help mailing list