#include "rmanager.h" #include #include #include #include #ifndef _WIN32 #include // For Linux. #endif #include #include #include RManager::RManager(QObject *parent) : QObject(parent) { const char *argv[] = {"RConsole"}; int argc = sizeof(argv) / sizeof(argv[0]); setlocale(LC_NUMERIC, "C"); // Required by Rembedded. #ifndef _WIN32 R_SignalHandlers = 0; // Don't let R set up its own signal handlers #endif Rf_initEmbeddedR(argc, (char**)argv); // Assign all callbacks. None in this reduced bug illustration. Rf_endEmbeddedR(0); } /** * @brief RManager::parseEval is the core of this console, sending commands to R. * @param line * @return */ int RManager::parseEval(const QString &line) { ParseStatus status; SEXP cmdSexp, cmdexpr, ret = R_NilValue; int i, errorOccurred, retVal=0; // Convert the command line to SEXP PROTECT(cmdSexp = Rf_allocVector(STRSXP, 1)); SET_STRING_ELT(cmdSexp, 0, Rf_mkChar(line.toLocal8Bit().data())); cmdexpr = PROTECT(R_ParseVector(cmdSexp, -1, &status, R_NilValue)); switch (status){ case PARSE_OK: // In this toy program, that's the only case that occurs. // Loop is needed here as EXPSEXP might be of length > 1 for(i = 0; ((i < Rf_length(cmdexpr)) && (retVal==0)); i++){ PROTECT(ret = R_tryEval(VECTOR_ELT(cmdexpr, i), R_GlobalEnv, &errorOccurred)); if (errorOccurred) { qDebug() << "An error occured during eval of" << line ; retVal = -1; } else { qDebug() << "No error occured during eval of" << line ; printf("EXAMPLE #1 Output: "); for (i=0; i