我正在尝试使用运算符 [] 读取 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!