Skip to content

how to call R function from my C++ program

5 messages · Yan Yu, Roger D. Peng, Brian Ripley +1 more

#
Hello, there,
  Is it possible to call R function from my C++ program?
If yes, How?  what function I should look up for that purpose?
If someone could provide some pointe, that would be very helpful..

THe reason I want to do that is that for my analysis, I need to do part of
the work in Matlab (I find the matrix operation in Matlab is very
convenient), and part of work in R, and I would like to connect
them automate the whole process using my C++ program..

THanks a lot in advance!
have a nice weekend,
yan
#
It is possible to call R functions from a C++ program.  I suggest reading
the manual "Writing R Extensions" available under the manuals section of
CRAN.

-roger
_______________________________
UCLA Department of Statistics
http://www.stat.ucla.edu/~rpeng
On Fri, 2 May 2003, Yan Yu wrote:

            
#
Hi,
I read the manual, it seems that i have the following two choices:
1) use "eval(R_fcall, rho)" in a C function.

They use an example called lapply2

in C program, it has:
SEXP  lapply2(SEXP list, SEXP fn, SEXP rho)
{
.....
PROTECT( R_fcall = lang2(fn, R_nilValue) );
eval(R_fcall, rho)
......
}

but i am a little confused here, it seems like the C function "lapply2()"
is supposed to be called from R, not to be run standalone??
My Q is: if I use this approach, Can I run a standalone C program in which
I call a R function using eval()?

2) R have some API entry points for C code.
in section 5.12, it says "it is possible to build Mathlib, can be built in
directory "src/namth/standalone", and the instructions are supposed in
"README" in the same directory.
I am wondering using the same procedure, can I build API entry points for
other R functions I want to use?

I have another Q, there is no "src" subdirectory in my R directory.
i use i386 rpm to install R in my redhat..
i think the version i have now is R-1.6.1-1.i386.rpm..
Any idea where can i find this src subdirectory?

thanks a lot,
yan
On Fri, 2 May 2003, Roger Peng wrote:

            
#
I am inferring that you are using Linux, although you didn't say. Some of 
my answers are specific to Unix/Linux.
On Sat, 3 May 2003, Yan Yu wrote:

            
Yes.  You can build R as a shared library (libR.so) and link against that.
However, you will need to initialize R: see the examples in the directory
tests/Embedding in the sources, and the background URL

http://developer.r-project.org/embedded.html
No.  (Those are not for R functions, but for parts of R's C API, and 
considerable work was done to separate out that part of the API to make 
it work standalone.)
In the sources.  You installed a binary version of R.  I suspect you will 
need to build from the sources to build libR.so.
1 day later
#
Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
The R Installation and Administration manual says,
  "Flag `--enable-R-shlib' [to `configure'] causes the make process to build R
  as a shared library, typically called `libR.so', and to take considerably
  longer, so you probably only want this if you will be using an application
  which embeds R."

Is there any disadvantage to using this flag, beside a longer "make" time?
Does it affect the normal running of R in any way?  I'm curious, because I'm
trying it for the first time now (with R-1.7.0).  Thanks.