Qt4、QProcess、R:标准输出中的垃圾,行较长
我正在为 R 开发另一个 GUI 前端(主要是在或多或少相关的材料上研究 Qt4),并且我偶然发现了所涉及组件的不寻常行为。 如果我尝试通过 QProcess 将一行 76 个字符或更长的字符写入 R 的标准输入,则输出(命令的回显)将返回并包含垃圾内容,并且该行的部分内容将被重复。例如:
freq.some <- recode(freq, "'some' = 'no'; 'all or most' = 'yes'; else = NA");
时,会出现回显
freq.some <- recode(freq, "'some' = 'no'; 'all or most' = 'yes'; else = NA"
< "'some' = 'no'; 'all or most' = 'yes'; else =
NA") ;
当我尝试在 QByteArray 级别上处理输出
,但异常已经存在。我的 Qt 版本是 4.6.3,R v. 2.11.1,Debian Squeeze。相关代码片段如下:
这是我启动 R 进程的方式:
arrr = new QProcess(this);
QString program = "R --interactive --no-readline";
arrr->start(program, QProcess::Unbuffered | QProcess::ReadWrite);
这是我将命令写入 R 进程的方式:
QString cmd = ui->lineEdit->displayText();
QString tmp = cmd + "\n";
arrr->write(tmp.toUtf8().data());
这是我读取进程输出的方式:
QByteArray output;
QTextStream *ts = new QTextStream(&output);
output = arrr->readAllStandardOutput();
QString r_output = ts->readAll();
在 readRead() 信号发生时从进程读取。
如果我的问题不符合本网站可接受的标准,我事先表示歉意。谢谢。
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 line 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我担心从长远来看,您可能会发现通过管道输入 R 并读取标准输出相当麻烦。我建议您可以考虑一些替代方案:
Rserve 提供您通过以下方式连接的无头 R 服务器TCP/IP;存在示例 C++ 客户端
按照“编写 R 扩展”手册进行直接 R 嵌入(但您会下拉到大量相当裸机的 C 代码)
RInside 使用同级包Rcpp 包装 R API,以便嵌入更高抽象的 C++ 中。另外,我最近添加了一个详细的示例,说明如何从 Qt 执行此操作,该示例现在已在 SVN 中(但尚未提供)在 CRAN 上);还有一个
有关它的详细博客文章。
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.