您好,登錄后才能下訂單哦!
本篇內容主要講解“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交互方法是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。