2013-08-01 115 views
0

我在R中繪製一個函數來繪製圖形。我需要使用C++程序來調用這個函數。如何在C++中調用並傳遞參數給R函數?

ř計劃(script.R)

genplot<-function(x,y,outfile) 
{ 
    plot(outfile) 
    plot(x,y) 
    dev.off() 
} 

C++程序(Run.cpp)

#include <iostream.h> 
using namespace std; 
int main() 
{ 
    int x[] = (1,2,3,4,5); 
    int y[] = (2,3,4,1,3); 
    genplot(x,y); #need to call r function by passing these 2 variables. 
    return 0; 
} 
+2

請看使用[RServe](http://www.rforge.net/Rserve/doc.html)。配備C++客戶端。 – Aert

+0

@Alert這真的很有用,但沒有示例顯示如何在C++中使用它。你可以建議或張貼任何相同的例子。 – Manish

回答

0

可以使用RInside。閱讀RInside頁面,我來這個,但我還沒有測試它。基本思想是使用Rcpp包裝器將數據傳遞給R,然後將函數作爲字符串進行評估。

#include <RInside.h> 
int main(int argc, char *argv[]) { 
    RInside R(argc, argv);    // create an embedded R instance 
    int x[] = (1,2,3,4,5); 
    int y[] = (2,3,4,1,3); 
    R.assign(x,"x"); 
    R.assign(y,"y"); 
    R["outfile"] = "output.txt"; 
    R.parseEvalQ("genplot(x,y,outfile)");   // eval init string, ignoring returns 
    exit(0); 
} 
相關問題