如何在C ++中调用参数并将参数传递给R函数?

时间:2013-08-01 03:18:51

标签: c++ r function

我在R中编写了一个用于绘制图形的函数。我需要使用C ++程序调用此函数。

R Program(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;
}

1 个答案:

答案 0 :(得分: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);
}