2

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 コマンド

必要に応じて返す

4

1 に答える 1

1

お望みならば

「[1]2」

2最後に、RInside からを受け取り、先頭に追加するフォーマット ルーチンをセットアップする必要があります[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()、エミュレートすることもできます。

于 2016-04-01T13:04:17.847 に答える