[R] Calling R functions within C/C++

Ian Schiller Ian.Schiller at clinepi.mcgill.ca
Thu Jan 5 21:23:19 CET 2012


> I am not sure if you have found Rcpp yet
I will take a look for sure!  Thanks a lot!

Ian

-----Original Message-----
From: Dirk Eddelbuettel [mailto:edd at debian.org] 
Sent: 5 janvier 2012 15:18
To: Ian Schiller
Cc: r-help at r-project.org
Subject: Re: [R] Calling R functions within C/C++


On 5 January 2012 at 15:04, Ian Schiller wrote:
| Hello everyone!
| 
| First of all, please note that  I'm working under Windows 7.
| 
| I have written a Gibbs sampler in R and I'm now in the process of translating it in C++ to increase the speed.  I'm relatively new to C++ and this question may be trivial, but my search so far have been unsuccessful.  In my Gibbs sampler I am using some basic R functions (like the rep function for example) and I was wondering if it was possible to call the R rep function (or any other existing R function) from within my C++ code.   (There may be an equivalent of the rep function already in C++ but my main interest here is to learn if there is a way to call the R function within C/C++)
| 
| I'm aware that you can call R random number generators as well as probability distribution functions with the  GetRNGstate() but what about other functions?
| 
| The sample of the C code below reveals what I would like to do regarding the rep function.
| 
| extern "C" {
|    void Gibbs_Sampler (double *vect.PI, int *iteration)
|     {
|                 int i;
|                 for (int i=0; i<*iteration; i++)
|                 {
|                   ...
| 
|                   PI.rep = rep(vec.PI,times=n)   // Can we call the R rep function such that this line of code (or any equivalent code) is valid?

No you cannot. A double* is not known to R. You need to convert to SEXP,
which can be automated if you used proper C++ classes (std::vector, say). 

|                   ...
|                  }
|     }
| }

I am not sure if you have found Rcpp yet, but it has, among other things, an
interface that makes calling R function very, very easy.  That is not the
same as fast. In most cases, you want a C++ equivalent.  And if you used
RcppArmadillo (a package which uses Rcpp to extend R to the very nice
Armadillo C++ classes) then you get functions for rep() and many other
utilities thrown in for free.

Rcpp is now used by 49 packages on CRAN, including for MCMC work. Have a long
at some, look at the Rcpp-introduction and Rcpp-FAQ vignettes and come on
over to the rcpp-devel list for follow-ups.

Dirk

| 
| Thanks,
| 
| 
| ******************************************************************************************************************************
| 
| IAN SCHILLER, M.Sc.
| 
| 
| 
| Statistical research assistant,
| 
| Division of Clinical Epidemiology, McGill University Health Center Assistant de recherche en statistiques, D�partement d'�pid�miologie Clinique, Centre Universitaire de Sant� Mcgill
| 
| 
| 
| Tel: 514 934 1934 ext. 36925
| 
| Email: ian.schiller at clinepi.mcgill.ca<mailto:ian.schiller at clinepi.mcgill.ca>
| 
| 
| 	[[alternative HTML version deleted]]
| 
| 
| ----------------------------------------------------------------------
| ______________________________________________
| R-help at r-project.org mailing list
| https://stat.ethz.ch/mailman/listinfo/r-help
| PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
| and provide commented, minimal, self-contained, reproducible code.

-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the R-help mailing list