朱庇特朱庇特“c——什么-r云娥与云娥同在",范仲淹?范仲淹。我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,是吴世盛。孙悟空是孙悟空吗“c——什么-r云娥与云娥同在""哎哎!
(c)云娥1 .刑事法院
#包括牡蛎
#包含因子
#包括侧面。h。包含林边。h
加工间隔时间:
intmain(intargc、char*argv[])
{
重新考虑(argc、argv);//哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼哼-r曰
stringmdstr//R你好
(c)在本报告所述期间:
cmd str=' m-1.23 ';
doubled 1=rcpp 3:为double(r . parse eval(cmd str));//是吗-r范仲淹与范仲淹d . 1
弯头D1=' D1 '端点;
cmdStr='str-\'HelloWorld!\ ';
字符串str=rcpp 3:作为字符串(r . parse eval(cmd str));//是吗-r范仲淹与范仲淹潜艇用热中子反应堆(submarine thermal reactor的缩写)
cout ' str=' strendl
cmd str=' B- false ';
boolb=rcpp 3: as pool(r . parse eval(cmd str));
coutboolalpha ' b=' bendl '
(c)在本报告所述期间,本报告所述期间内,本报告所述期间内,本报告所述期间内,本报告所述期间内,本报告所述期间内,本报告所述期间内,本报告所述期间内,本报告所述期间内所述
cmd str=' m-c(1.0 * 1:6)';
向量加倍2c=rcpp 3:作为向量加倍(r . parse eval(cmd str));//是吗-r嗨嗨嗨嗨嗨嗨嗨嗨c。非政府组织什么事向量(向量)何曰
端壁D2 vec[0]=' D2 vec[0];
端壁D2维克[1]=' D2维克[1];
S7-1200可编程控制器
;< 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交互方法是什么”有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/125496.html