Darren Obbard
2007-Jan-24 13:22 UTC
[R] Capturing output from external executables, in windows
Hi, Any help on the following would be much appreciated I wish to capture the output (currently going to console) from an external executable. The executable is successfully run using system("program -switch ") and the output printed to the DOS console. How do I capture this output? I have tried redirecting the output to a text file, and then reading this in system("program -switch > textfile.txt") data<-scan("textfile.txt") But this does not seem to work (the textfile.txt is not written). It does however work if I invoke the console to be permanent system("cmd /K program -switch > textfile.txt") data<-scan("textfile.txt") Unfortunately, this leaves me with an open console window I have to close manually. Is there a way of doing this (under windows) using system( ) or some other command? It appears that pipe( ) may do it, but I cannot understand the documentation. An example of the appropriate syntax would be an enormous help. Thanks in advance, Darren Darren.Obbard at ed.ac.uk -- Darren Obbard Institute of Evolutionary Biology Ashworth Labs Kings Buildings University of Edinburgh, UK
Henrique Dallazuanna
2007-Jan-24 13:45 UTC
[R] Capturing output from external executables, in windows
You can try: system("command", show.output.on.console=TRUE) On 24/01/07, Darren Obbard <darren.obbard@ed.ac.uk> wrote:> > Hi, > > Any help on the following would be much appreciated > > I wish to capture the output (currently going to console) from an > external executable. > > The executable is successfully run using > > system("program -switch ") > > and the output printed to the DOS console. > > How do I capture this output? I have tried redirecting the output to a > text file, and then reading this in > > system("program -switch > textfile.txt") > data<-scan("textfile.txt") > > But this does not seem to work (the textfile.txt is not written). It > does however work if I invoke the console to be permanent > > system("cmd /K program -switch > textfile.txt") > data<-scan("textfile.txt") > > Unfortunately, this leaves me with an open console window I have to > close manually. > > Is there a way of doing this (under windows) using system( ) or some > other command? It appears that pipe( ) may do it, but I cannot > understand the documentation. > > An example of the appropriate syntax would be an enormous help. > > Thanks in advance, > > Darren > > Darren.Obbard@ed.ac.uk > > -- > > Darren Obbard > Institute of Evolutionary Biology > Ashworth Labs > Kings Buildings > University of Edinburgh, UK > > ______________________________________________ > 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. >-- Henrique Dallazuanna [[alternative HTML version deleted]]
Duncan Murdoch
2007-Jan-24 14:13 UTC
[R] Capturing output from external executables, in windows
On 1/24/2007 8:22 AM, Darren Obbard wrote:> Hi, > > Any help on the following would be much appreciated > > I wish to capture the output (currently going to console) from an > external executable. > > The executable is successfully run using > > system("program -switch ") > > and the output printed to the DOS console. > > How do I capture this output? I have tried redirecting the output to a > text file, and then reading this in > > system("program -switch > textfile.txt") > data<-scan("textfile.txt") > > But this does not seem to work (the textfile.txt is not written).The redirection character ">" is normally handled by the shell. The system() function is low level, so it will pass it directly to your program. > It> does however work if I invoke the console to be permanent > > system("cmd /K program -switch > textfile.txt") > data<-scan("textfile.txt") > > Unfortunately, this leaves me with an open console window I have to > close manually.Use the /C option instead of /K; it executes and then terminates. Or use the "intern" or "show.output.on.console" arg to system(). Duncan Murdoch> > Is there a way of doing this (under windows) using system( ) or some > other command? It appears that pipe( ) may do it, but I cannot > understand the documentation. > > An example of the appropriate syntax would be an enormous help. > > Thanks in advance, > > Darren > > Darren.Obbard at ed.ac.uk >
Prof Brian Ripley
2007-Jan-24 14:44 UTC
[R] Capturing output from external executables, in windows
Is this for RGui under Windows? I will assume so (but 'in windows' is not unambiguous, and there are alternative front-ends like Rterm). Have you consulted the help page for system()?: this is precisely what 'show.output.on.console' is for. You cannot redirect in system (it does say so on the current help page): you need to use shell(). On Wed, 24 Jan 2007, Darren Obbard wrote:> Hi, > > Any help on the following would be much appreciated > > I wish to capture the output (currently going to console) from an > external executable. > > The executable is successfully run using > > system("program -switch ") > > and the output printed to the DOS console. > > How do I capture this output? I have tried redirecting the output to a > text file, and then reading this in > > system("program -switch > textfile.txt") > data<-scan("textfile.txt") > > But this does not seem to work (the textfile.txt is not written). It > does however work if I invoke the console to be permanent > > system("cmd /K program -switch > textfile.txt") > data<-scan("textfile.txt") > > Unfortunately, this leaves me with an open console window I have to > close manually. > > Is there a way of doing this (under windows) using system( ) or some > other command? It appears that pipe( ) may do it, but I cannot > understand the documentation. > > An example of the appropriate syntax would be an enormous help.system("program -switch ", show.output.on.console=TRUE) could it be any easier? (Well, in the next version of R that will be default, so a little.)> Thanks in advance, > > Darren > > Darren.Obbard at ed.ac.uk > >-- 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