[R] C++ and R interface

Deepankar Basu basu.15 at osu.edu
Tue Nov 6 18:26:12 CET 2007


Thanks for the help.

On Tue, 2007-11-06 at 14:48 -0200, Alberto Monteiro wrote:
> Deepankar Basu wrote:
> > 
> > I am trying to do an ML estimation in R. My likelihood function has
> > several nested loops and so it takes a lot of time (days when I use the
> > genetic algorithm for optimization) for the optimization to finish.
> > Unable to avoid loops, I am thinking of writing the likelihood function
> > in C++ and calling it from within R when using *optim()*. I found 
> > that one can call C functions (once they have been compiled) from 
> > within R with
> > 
> > > dyn.load("file.so")
> > 
> > and
> > 
> > > .C("function", ...)
> > 
> > Can the same be done for C++ code?
> > 
> If it can be done in C, it probably (P > 99%) can be done in C++, 
> because C++ can call C functions. Just write the C++ code as:
> 
> extern "C" int myfunction(int x1, int x2)
> {
>   // write C++ code here
>   return rval;
> }
> 
> Alberto Monteiro
>



More information about the R-help mailing list