在类/方法之间传递RInside的'R'实例作为参数 [英] Passing RInside's 'R' instance as a parameter between classes/methods

查看:129
本文介绍了在类/方法之间传递RInside的'R'实例作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Rcpp和RInside来集成 R C ++ .我们有一个复杂但设计良好的C ++架构,我发现很难从1个函数中访问R.是否可以将R实例传递给不同的类/函数,以获得更多的OOP设计?如果是,是否有任何示例?

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


我当前注意到的是,每次对foo的调用都可能会创建RInside的新实例.


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

谢谢-伊贡

推荐答案

是的,正如我之前写给 Qt 内.

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.

我实例化main()中的R,然后将对此对象的引用传递给使用它的类.这似乎是一个正确的模型,因为我们需要确保仅运行一个R实例(R不处于多线程状态下,如果您需要更多实例,请考虑

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.

此示例为现在在SVN中,在我的盒子上看起来像这样:

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.

这篇关于在类/方法之间传递RInside的'R'实例作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆