I'm using Rcpp and RInside to run some commands into R. I've made a personal GUI (in Qt) which sends commands, and I would like to recover the result in std::string format.
Example :
$ 1 + 1
The result is :
[1] 2
And I want to have this string :
"[1] 2"
I already check string cast with "as" and "as_string", but the cast is invalid cause of intern return type in R.
Is it possible to read R console output or something else ?
EDIT:
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 allow to display a basic R console like this
$ R command
return if needed