En passant l'instance « R » de RInside en tant que paramètre entre les classes / méthodes

StackOverflow https://stackoverflow.com/questions/5357765

  •  27-10-2019
  •  | 
  •  

Question

J'utilise CRPP et RInside, pour intégrer R et C ++ . Nous avons une architecture complexe, mais bien conçu C ++, et je trouve qu'il est difficile d'accès R à partir de seulement 1 fonction. Est-il possible de passer l'instance de R à différentes classes / fonctions, pour obtenir une conception plus POO? Si oui, y at-il des exemples?

Pour élaborer la requête, Je veux dire que quelque chose comme ça,

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

Qu'est-ce que je remarque actuellement est que chaque appel à foo, crée probablement une nouvelle instance de RInside.

Merci - Egon

Était-ce utile?

La solution

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:

enter image description here

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top