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 at geodecapital.com This e-mail, and any attachments hereto, are intended for us...{{dropped}}
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