Rcpp と RInside を使用して、いくつかのコマンドを R に実行しています。コマンドを送信する個人用の GUI (Qt で) を作成しました。結果を std::string 形式で復元したいと考えています。
例 :
$1 + 1
結果は次のとおりです。
[1] 2
そして、私はこの文字列が欲しい:
「[1]2」
「as」と「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 コマンド
必要に応じて返す