[Rcpp-devel] Question regarding integrating preexisting C++ code via Rcpp

Dirk Eddelbuettel edd at debian.org
Mon Oct 10 16:49:56 CEST 2011


On 10 October 2011 at 11:09, Simon Fuller wrote:
| I am new to Rcpp, have some experience with both R and C++, but very little
| experience with integrating languages.
| 
| I hope to use Rcpp to develop code over the next few years. However, to begin
| with, I have some preexisting C++ classes that I would like to make available
| to R.
| 
| I have read the documentation that I can find, but I do not know which is the
| best approach to take.
| 
| I only, in principle, need one method to interact with the classes - there then
| follows a fairly arduous process using different tree structures each of which
| have aggregate classes, but only a single double needs to be returned to R.

There are two basic approaches:

 -- Just write the function, test it via inline, eventually turn it into a
    package. There are hundreds of working examples in the docs and in
    several packages.

    I would start here. Understanding this helps with the alternative.

 -- Use, as Romain suggested, the Rcpp modules approach.  You may only a
    declaration of a few lines, but understanding the details is IMHO more
    advanced. There is a vignette, and there are several CRAN packages using
    this approach.

| I was therefore hoping to simply call a C++ wrapper function in-line, remotely,
| which in turn called my classes, but that does not work. I get an:
| "expected unqualified-id before ‘extern’ " error after my includes, at:
| RcppExport SEXP..  at the start of my method declaration. (I can post code if
| this approach is not a complete dead end)

Do post code. Mock up your class if you want to be complete.
 
| So my two questions are: is there a quick way of prototyping this? i.e. to get
| R to invoke my classes through an rcpp intermediary.

Yes.
 
| If there is not a quick way to do this, what is the best approach? Do I need
| to, and if so how do I, make my classes accessible to Rcpp. Because my whole
| hope here is to use preexisting code as swiftly as possible, 'easiest' and
| 'quickest' are almost synonyms for 'best' in my question.
| 
| If someone could point me in the direction of some example code where Rcpp
| invokes user classes this would be great.

We have eight vignettes in the package.

The question has been answered dozen times on this list.
 
| My apologies if my question has been covered before, but I would really
| appreciate some advice on what is and is not possible, and some strategic
| advice on the best (easiest and quickest) approach.

See above.  Rephrase the question if need, and post code if stuff fails.

Dirk
 
| Thank you for your help. It is deeply appreciated.
| 
| Simon
| 
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-- 
"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 Rcpp-devel mailing list