RInside:无法将R [" R.version.string"]读为字符串

时间:2014-11-27 10:10:51

标签: rcpp rinside

我正在尝试使用operator []读取R.version.string的值。结果抛出异常。相反,使用R.parseEval(“R.version.string”)就可以了。下面是修改后的示例rinside_sample0.cpp。

#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);
}

获得的输出是:

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

1 个答案:

答案 0 :(得分:0)

此问题已在https://github.com/eddelbuettel/rinside/issues/9上解决。变量R.version.string在基本环境中是可见的,可以像这样检索它:

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