views:

240

answers:

1

Hi Experts,

I would like to execute a R script simply as

R --file=x.R

It runs well on the command line. However when I try the system call in C++ by

QProcess::execute("R --file=x.R");

or

system("R --file=x.R");

the program R runs and quits but I can't see the output the program is supposed to generate. If a program uses no stdout (such as R), how do I fetch the output after a system call either as a output file or in the program's own console?

Thanks for your time.

+1  A: 

QProcess allows you to capture the output of the command you run; but not through the static function call you used. Instead, try something like this:

QProcess process;
process.start( "R --file=x.R" );
process.waitForFinished();
QByteArray output = process.readAllStandardOutput();
QByteArray error = process.readAllStandardError();

Of course, to do things properly, you might want to start the process, then do the rest of the code in a slot connected to the process object's finished signal. You can also read from the standard output or error incrementally, if you so desire. Or you can set objects to receive the output or error as input to those objects.

Caleb Huitt - cjhuitt
Thanks. But the "output" only holds the welcome message from R but x.R is either ignored or executed but produced no result. "R --file=x.R" still works on command line.
SYK
@SYK: My next guess is that it might be something that R is doing to detect what sort of output possibilities it has, and only output if it has a terminal to print to. However, that probably doesn't help you much.
Caleb Huitt - cjhuitt