C++与R交互方法是什么
本篇内容主要讲解“C++与R交互方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C++与R交互方法是什么”吧!
一、C++代码1.cpp
#include <iostream> #include <vector> #include <RInside.h> using namespace std; int main(int argc,char* argv[]) { RInside R(argc,argv);//创建一个R环境 string cmdStr;//R命令行变量 cout << "******* test R basic data type *******" << endl; cmdStr = "m <- 1.23"; double d1 = Rcpp::as<double>(R.parseEval(cmdStr));//将R命令结果解析赋值给d1 cout << "d1 = " << d1 << endl; cmdStr = "str <- \"Hello World!\""; string str = Rcpp::as<string>(R.parseEval(cmdStr));//将R命令结果解析赋值给str cout << "str = " << str << endl; cmdStr = "b <- FALSE"; bool b = Rcpp::as<bool>(R.parseEval(cmdStr)); cout << boolalpha << "b = " << b << endl; cout << "******* test R vector *******" << endl; cmdStr = "M <- c(1.0*1:6)"; vector<double> d2Vec = Rcpp::as< vector<double> >(R.parseEval(cmdStr));//将R命令向量赋值转换到C++的vector中 cout << "d2Vec[0] = " << d2Vec[0] << endl; cout << "d2Vec[1] = " << d2Vec[1] << endl; cout << "d2Vec[2] = " << d2Vec[2] << endl; cout << "d2Vec[3] = " << d2Vec[3] << endl; cout << "d2Vec[4] = " << d2Vec[4] << endl; cout << "d2Vec[5] = " << d2Vec[5] << endl; cout << "******* test R matrix *******" << endl; cmdStr = "M <- matrix(1:6,2,3)";//创建矩阵 vector<double> d3Vev = Rcpp::as< vector<double> >(R.parseEval(cmdStr)); cout << "d3Vev[0] = " << d3Vev[0] << endl; cout << "d3Vev[3] = " << d3Vev[3] << endl; cout << "d3Vev[5] = " << d3Vev[5] << endl; cout << "******* test R list *******" << endl; cmdStr = "lst <- list(name = \"Fred\",wife = \"Mary\",no.child = 3,child.ages = c(12,15,17))";//创建列表数据 Rcpp::List myList= R.parseEval(cmdStr); cout << "myList[\"name\"] = " << Rcpp::as<string>(myList["name"]) << endl; cout << "myList[\"wife\"] = " << Rcpp::as<string>(myList["wife"]) << endl; cout << "myList[\"no.child\"] = " << Rcpp::as<int>(myList["no.child"]) << endl; Rcpp::IntegerVector ageIntVec = Rcpp::as<Rcpp::IntegerVector>(myList["child.ages"]); cout << "ageIntVec[0] = " << ageIntVec[0] << endl; cout << "ageIntVec[1] = " << ageIntVec[1] << endl; cout << "ageIntVec[2] = " << ageIntVec[2] << endl; return 0; }
二、安装R包
$ R
> install.packages("Rcpp")
> install.packages("Rcpp11")
> install.packages("RInside")
这里R的扩展包都安装在/usr/local/lib/R/site-library/。如普通用户没有对该目录写的权限,
执行chmod a+rw /usr/local/lib/R/site-library
为了方便g++找到对应的库建立软链接:
sudo ln -s /usr/local/lib/R/site-library/RInside/lib/libRInside.so /usr/lib/libRInside.so
sudo ldconfig -v
三、编译:
g++ -I /usr/local/lib/R/site-library/RInside/include -I /usr/local/lib/R/site-library/Rcpp/include -I /usr/share/R/include 1.cpp -lR -lRInside
或者二部分后面动态库位置不动,用下面命令编译(编译时指定动态库位置):
g++ -I /usr/local/lib/R/site-library/RInside/include -I /usr/local/lib/R/site-library/Rcpp/include -I /usr/share/R/include -o testR testR.cpp -L /usr/local/lib/R/site-library/RInside/lib -lR -lRInside -Wl,-rpath=/usr/local/lib/R/site-library/RInside/lib
四、运行:
$ ./a.out ******* test R basic data type ******* d1 = 1.23 str = Hello World! b = false ******* test R vector ******* d2Vec[0] = 1 d2Vec[1] = 2 d2Vec[2] = 3 d2Vec[3] = 4 d2Vec[4] = 5 d2Vec[5] = 6 ******* test R matrix ******* d3Vev[0] = 1 d3Vev[3] = 4 d3Vev[5] = 6 ******* test R list ******* myList["name"] = Fred myList["wife"] = Mary myList["no.child"] = 3 ageIntVec[0] = 12 ageIntVec[1] = 15 ageIntVec[2] = 17
到此,相信大家对“C++与R交互方法是什么”有了更深的了解,不妨来实际操作一番吧!这里是蜗牛博客网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。版权声明:如无特殊标注,文章均为本站原创,转载时请以链接形式注明文章出处。
评论