eddelbuettel/rinside

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

Closed this issue · 2 comments

Hi, 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);
}

To a first approximation, you need to parse and eval a valid R expression. As such, your approach using [ ... ] is just wrong -- that is not how it works.

The second approach is correct, and you say it works. So I am closing this -- there is no issue here.

I found another way without parsing and evaluating an R expression:

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