2

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

Community
  • 1
  • 1
C. Dupont
  • 21
  • 2
  • Presumably your project consists of several files as it is a Qt GUI application, but could you include the relevant sections of code that handle this command IO? – nrussell Apr 01 '16 at 11:47
  • 1
    I've made it, thanks for your answer. I hope you could help me :) – C. Dupont Apr 01 '16 at 12:51

1 Answers1

1

If you want

"[1] 2"

you need to setup a formatting routine at your end that receives the 2 from RInside and prepends [1] (and ditto for the other lines). This is just what print() does in R:

edd@max:~$ R --slave -e 'print(1+1)'
[1] 2
edd@max:~$ R --slave -e 'cat(1+1, "\n")'
2 
edd@max:~$

I actually prefer cat() on my results but print() can be emulated too.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 1+1 return 2, but on R console display "[1] 2", it's true ? – C. Dupont Apr 01 '16 at 14:30
  • *Because of the way R (as an interactive environment) prints*. If you call `int R.parseEvalQ("1+1")` you get an `int` with the value of 2. – Dirk Eddelbuettel Apr 01 '16 at 14:37
  • So i need some functions to detect and cast each type! Thanks :) – C. Dupont Apr 01 '16 at 14:54
  • No you don't. You need to sit down and read and compiled and experiment with a bunch of the existing examples. You are confused about absolute pre-beginners issues, and I am afraid you are not using your time efficiently but firing off one-liners here. Do some background reading, learn about Rcpp, run examples. – Dirk Eddelbuettel Apr 01 '16 at 15:24