I'm working on a yet another one GUI-frontend for R (mainly to study Qt4 on a more or less relevant material), and I've stumbled upon an unusual behavior of components involved. If I try to write a line of 76 characters or longer to R's stdin via QProcess, the output (echo of the command) returns with garbage inclusions and with parts of the l开发者_StackOverflow中文版ine being repeated. For example:
freq.some <- recode(freq, "'some' = 'no'; 'all or most' = 'yes'; else = NA");
is echoed as
freq.some <- recode(freq, "'some' = 'no'; 'all or most' = 'yes'; else = NA"
< "'some' = 'no'; 'all or most' = 'yes'; else =
NA") ;
I tried working with the output right on the QByteArray level, but the anomaly is already there.
My Qt version is 4.6.3, R v. 2.11.1, Debian Squeeze. Relevant code snippets follow:
This is how I start R's process:
arrr = new QProcess(this);
QString program = "R --interactive --no-readline";
arrr->start(program, QProcess::Unbuffered | QProcess::ReadWrite);
This is how I write the command to R's process:
QString cmd = ui->lineEdit->displayText();
QString tmp = cmd + "\n";
arrr->write(tmp.toUtf8().data());
This is how I read the output of the process:
QByteArray output;
QTextStream *ts = new QTextStream(&output);
output = arrr->readAllStandardOutput();
QString r_output = ts->readAll();
Reading from the process occurs upon readyRead() signal.
I apologize beforehand if my question does not conform to the accepted standards of this site. Thank you.
I fear that in the long run you may find piping into R and reading the standard output to be rather troublesome. I suggest a few alternatives you could look into:
Rserve provides a headless R server you connect to via tcp/ip; a sample C++ client exists
Direct R embedding as per the `Writing R Extensions' manual (but you'd drop down to a lot of fairly bare-metal C code)
RInside wraps the R API for embedding in much higher abstraction C++ using the sibbling package Rcpp. Plus, I recently added a detailed example of how to do that from Qt which is now in SVN (but not yet on CRAN); there is also a detailed blog post about it.
精彩评论