[R] cat() to STDERR

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Apr 20 18:38:09 CEST 2007

On Fri, 20 Apr 2007, Peter Dalgaard wrote:

> ivo welch wrote:
>> Dear R wizards---I read Brian Ripley's post from 2004 which said that
>> it was not possible to print to STDERR.  Alas, I have more modest
>> needs.  I was wondering if it was possible to just send a string to
>> STDERR with cat() while in CMD BATCH mode.
>> Is it not possible to open STDERR in R?  (Or does R use STDERR for
>> itself and redirect it into the output stream?)
>> This would be on a standard Unix (gentoo) system and R 2.4.x.
> $ echo 'sink(stderr());cat("Goodbye, cruel World!\n");sink()' | R -q
> --vanilla 2> tictactoe
>> sink(stderr());cat("Goodbye, cruel World!\n");sink()
> $ cat tictactoe
> Goodbye, cruel World!

Is there a reason not to simply use

cat("Goodbye, cruel World!\n", file = stderr())

here?  (Of course, stderr() is not necessarily STDERR, but in a Linux 
terminal it is.)

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

More information about the R-help mailing list