RInside: can not read R["R.version.string"] as string

56 views Asked by At

I'm trying to read the value of R.version.string using the operator []. As a result an exception is thrown. Instead, with R.parseEval("R.version.string") is OK. Below is the example rinside_sample0.cpp modified showing the issue.

#include <RInside.h>                    // for the embedded R via RInside

int main(int argc, char *argv[]) {

    RInside R(argc, argv);              // create an embedded R instance 

    try {
    std::string versionKO = R["R.version.string"];
    } catch(std::exception& ex) {
    std::cerr << "Exception caught: " << ex.what() << std::endl;
    } catch(...) {
    std::cerr << "Unknown exception caught" << std::endl;
    }

    std::string versionOK = R.parseEval("R.version.string");
    std::cout << versionOK << std::endl;

    R["txt"] = "Hello, world!\n";   // assign a char* (string) to 'txt'

    R.parseEvalQ("cat(txt)");           // eval the init string, ignoring any returns

    exit(0);
}

The output obtained is:

Exception caught: expecting a string
R version 3.1.2 (2014-10-31)
Hello, world!
1

There are 1 answers

0
Jorge On

This issue has been solved on https://github.com/eddelbuettel/rinside/issues/9. The variable R.version.string is visible in the base environment and it can be retrieved like this:

Rcpp::Environment baseEnv("package:base");
std::string versionR = baseEnv["R.version.string"];