[R] Capturing stderr from system()

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Aug 7 21:28:31 CEST 2006


On Mon, 7 Aug 2006, McGehee, Robert wrote:

> Hello,
> I have a system command that occasionally fails and writes output to
> standard error, which R will print to the screen when ignore.stderr =
> FALSE.

No, R does not print to the screen, your shell does. You can capture shell 
and command errors by

	system("command 2>&1", intern=TRUE) 

or whatever the appropriate syntax for your shell is.  E.g.

> system("BadCommand 2>&1", intern=TRUE)
[1] "sh: BadCommand: command not found"

> For example:
> > system("BadCommand")
> sh: line 1: BadCommand: command not found

(My shell does not give `line 1' here.)

> I would like to know if the above command fails, and can presumably do
> this by parsing the stderr message that R prints to the screen. My
> (hopefully simple) problem is that I can't figure out how to capture
> this output.

You are starting from a false assertion ....

[...]

> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

PLEASE do note what it says about where to send programming questions.

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