[R] Capturing stderr from system()

McGehee, Robert Robert.McGehee at geodecapital.com
Mon Aug 7 21:00:32 CEST 2006

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

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

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.

I tried intern = TRUE, capture.output, and sink (with either/both type =
"message" and type = "output"). I'm perplexed why sink doesn't get this
output (since it's printed to the screen), so wanted to ask the R
community how to grab the error message generated by trying to run

To be clear, below is the sink() syntax I tried. Notice the output of
the sink is character(0) rather than the line "sh: line 1: BadCommand:
command not found".

> conn <- sink(z <- tempfile())
> sink(conn, type = "message")
> system("BadCommand")
sh: line 1: BadCommand: command not found
> sink(type = "message")
> sink()
> readLines(z)

Thanks in advance,

> R.version
platform       i686-pc-linux-gnu                        
arch           i686                                     
os             linux-gnu                                
system         i686, linux-gnu                          
status         Patched                                  
major          2                                        
minor          3.1                                      
year           2006                                     
month          07                                       
day            27                                       
svn rev        38721                                    
language       R                                        
version.string Version 2.3.1 Patched (2006-07-27 r38721)

Robert McGehee
Quantitative Analyst
Geode Capital Management, LLC
53 State Street, 5th Floor | Boston, MA | 02109
Tel: 617/392-8396    Fax:617/476-6389
mailto:robert.mcgehee at geodecapital.com

This e-mail, and any attachments hereto, are intended for us...{{dropped}}

More information about the R-help mailing list