Passing RInside's 'R' instance as a parameter between classes/methods

假如想象 提交于 2019-12-21 19:54:49

问题


I have been using Rcpp and RInside, to integrate R and C++. We have a complex, yet well designed C++ architecture, and I am finding it difficult to access R from within just 1 function. Is it possible to pass the R instance to different Classes / functions, to get a more OOP design ? If yes, are there any examples ?

To elaborate the query, I want to say that something like this,

void foo(RInside& R0, int& x0)
{
 R0.assign(x0,"totalSum");
}
void foo2(RInside& R0, int& y0)
{
  R0.assign(y0,"temp");
   R0.parseEvalQ("totalSum = totalSum + temp"); 
 }
int main(int argc, char *argv[])
{   
  RInside R(int argc, char *argv[]);
  int x=10, y = 11;
  foo(R,x);
  foo2(R,y);
  return 0;
}

What I am currently noticing is that each call to foo, probably creates a new instance of RInside.

Thank you - Egon


回答1:


Yes, and as I wrote earlier to the rcpp-devel list (which you should follow if you care about Rcpp and RInside) I just added an example to RInside which embeds it inside Qt.

I instantiante R in main() and then pass a reference to this object to the class using it. That seems like a proper model as we need to make sure that only one R instance is running (with R not being multithreaded etc---if you need more instances, consider Rserve.

This example is now in SVN and looks like this on my box:

I quite like it as it lets you play around with mixtures, spread the central location of both draws etc and see at which bandwidth you no longer differentiate between two humps of the estimated density.



来源:https://stackoverflow.com/questions/5357765/passing-rinsides-r-instance-as-a-parameter-between-classes-methods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!