[R] Call R program from C++ code

Dirk Eddelbuettel edd at debian.org
Mon Jul 30 00:17:27 CEST 2007

On Sun, Jul 29, 2007 at 04:35:51PM -0400, Feng Qiu wrote:
>                I'm developing an application program using C++. From my C++
> code, I would call some R program I have written. I' wondering if R provide
> some compiler that can compile R program into executable program. I searched

That does not exist to the best of my knowledge.

>                I might be wrong that R doesn't have complier. What I'm
> trying to do is to call R program from C++ code. Any help is highly
> appreciated!

As you probably know, C++ can 'call' other object code that is linked
to it.  As for 'calling R', the easiest way is to call an R script
using the system() call -- but it is also the most tedious way as you
to write the inout data to file, and then read the result data back
in.  But it is a start, and it may be easiest to debug.

A more advanced method would to use Rserve to run a 'headless' R
service to which your C++ program can connect over the network. But
there you need to be already somewhat familiar with the underlying
C/C++ representation of R object. Rserve has simple examples.

Next, you can actually embed R inside your C++ application, but that
is more advanced.

In any event, you may also want to consider the RcppTemplate package
which has a host of examples about how to get R and C++ to work better
together (without forcing you to use C).

The 'Extending R' manual from your R installation is a good starting
point for most of this.

Hope this helps, Dirk

Three out of two people have difficulties with fractions.

More information about the R-help mailing list