[R] Capturing stderr from system()

From: McGehee, Robert <Robert.McGehee_at_geodecapital.com>
Date: Tue 08 Aug 2006 - 05:00:32 EST


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. 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 "BadCommand".

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)
character(0)

Thanks in advance,
Robert

> 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@geodecapital.com

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



R-help@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 and provide commented, minimal, self-contained, reproducible code. Received on Tue Aug 08 14:20:44 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 08 Aug 2006 - 16:17:43 EST.

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