Qt4、QProcess、R:标准输出中的垃圾,行较长

发布于 2024-10-28 18:27:14 字数 1193 浏览 3 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅冕 2024-11-04 18:27:14

我担心从长远来看,您可能会发现通过管道输入 R 并读取标准输出相当麻烦。我建议您可以考虑一些替代方案:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文