C++與R交互(RInside)

1、C++代碼1.cppios

#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;
}

2、安裝R包ide

$ Rspa

> install.packages("Rcpp")命令行

> install.packages("Rcpp11")code

> install.packages("RInside")cmd

這裏R的擴展包都安裝在/usr/local/lib/R/site-library/。如普通用戶沒有對該目錄寫的權限,string

執行chmod a+rw /usr/local/lib/R/site-libraryit

爲了方便g++找到對應的庫創建軟連接:io

sudo ln -s /usr/local/lib/R/site-library/RInside/lib/libRInside.so /usr/lib/libRInside.so編譯

sudo ldconfig -v

3、編譯:

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

4、運行:

$ ./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
相關文章
相關標籤/搜索