使用RInside读取R控制台
Read R console with RInside
我正在使用Rcpp和RInside将一些命令运行到R中。我制作了一个发送命令的个人GUI(在Qt中),我想以std::string格式恢复结果。
示例:
$1+1
结果是:
[1] 2
我想要这个字符串:
"[1] 2〃;
我已经用";作为";以及";as_string";,但由于R.中的内部返回类型,强制转换无效
是否可以读取R控制台输出或其他内容?
编辑:
void RppMainWindow::runLineOnScriptCursor() {
std::string line = script->getCodeEditor()->lineOnCursor();
if ( line.empty() || line == INVALID ) {
return;
}
RCommand cmd (script->getConsoleViewer(), r);
cmd.execute(line);
}
void RCommand::execute(std::string commande) {
std::string res = executeOnR(commande);
viewer->addEntry(commande, res);
}
void ConsoleViewer::addEntry(std::string command, std::string result) {
this->moveCursor(QTextCursor::End);
QTextCharFormat format;
format.setFontWeight(QFont::DemiBold);
format.setForeground(QBrush(QColor("red")));
this->mergeCurrentCharFormat(format);
std::string tmp = "> " + command + "n";
this->insertPlainText(QString(tmp.c_str()));
this->moveCursor(QTextCursor::End);
format.setFontWeight(QFont::Normal);
format.setForeground(QBrush(QColor("blue")));
this->mergeCurrentCharFormat(format);
if ( ! result.empty() ) {
result += "n";
}
this->insertPlainText(QString(result.c_str()));
}
ConsoleViewer允许显示类似的基本R控制台
$R命令
如果需要返回
如果您想要
"[1]2"
您需要在您的端部设置一个格式化例程,该例程接收来自RInside的2
并预处理[1]
(其他行也是如此)。这正是print()
在R:中的作用
edd@max:~$ R --slave -e 'print(1+1)'
[1] 2
edd@max:~$ R --slave -e 'cat(1+1, "n")'
2
edd@max:~$
事实上,在我的结果中,我更喜欢cat()
,但print()
也可以被模仿。
相关文章:
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- 从文件读取并通过矢量输出到控制台
- 反转从控制台读取的文本
- 如何使用 cin 将整数从控制台读取到向量中
- 如何从 c++ 控制台读取未知行数(允许空行)上的输入
- 硬编码字符串与强制转换为 PUCHAR 并打印到控制台时从控制台读取的字符串的结果不同
- C++ 从控制台读取整数数组
- 指向C 中的结构 - 从控制台读取
- 如何从文件中一次读取一个输入,类似于在c++中使用cin/scanf从控制台读取输入
- 从控制台读取字符
- 从控制台C++读取随机整数的更快方法
- 从控制台读取 2D 数组
- boost::program_options从控制台读取一个整数数组
- 从控制台读取字符串并将其存储在数组中
- istream::readsome不从控制台读取?
- 从控制台读取字符串以及空格
- 正在从控制台读取数据
- 从控制台读取:运算符>>用于模板类中的枚举
- C++控制台读取不确定数量的输入,并将它们放入数组中
- 如何在不C++中添加换行符的情况下从控制台读取整数