您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“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);//創(chuàng)建一個R環(huán)境 string cmdStr;//R命令行變量 cout << "******* test R basic data type *******" << endl; cmdStr = "m <- 1.23"; double d1 = Rcpp::as<double>(R.parseEval(cmdStr));//將R命令結(jié)果解析賦值給d1 cout << "d1 = " << d1 << endl; cmdStr = "str <- \"Hello World!\""; string str = Rcpp::as<string>(R.parseEval(cmdStr));//將R命令結(jié)果解析賦值給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命令向量賦值轉(zhuǎn)換到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)";//創(chuàng)建矩陣 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))";//創(chuàng)建列表數(shù)據(jù) 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/。如普通用戶沒有對該目錄寫的權(quán)限,
執(zhí)行chmod a+rw /usr/local/lib/R/site-library
為了方便g++找到對應(yīng)的庫建立軟鏈接:
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
或者二部分后面動態(tài)庫位置不動,用下面命令編譯(編譯時指定動態(tài)庫位置):
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交互方法是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。