[Rcpp-devel] Rcpp

Dirk Eddelbuettel edd at debian.org
Mon May 10 20:41:36 CEST 2010


Hi Jean-Eudes,

Thanks for your interest in Rcpp.

On 10 May 2010 at 14:09, Jean-Eudes Dazard wrote:
| Here is my beginner?s question:
| 
| We want to interface an existing C++ subroutine to R. The subroutine can be
| wrapped up in a C++ program, compiled with standard gcc, and run e.g. under GNU
| /Linux 2.6.18-128.1.14.el5. It accepts several arguments to it.
| 
| (i) Using the .Call interface (with SEXP types) seems the best option since we

Yes. Do not use .C() unless you must. .Call() is preferable.

| want to pass matrices, vectors, scalars to it from R. However, the existing C++
| subroutine is ~ hundreds of line long. To use Rcpp, it seems unavoidable (yet
| purpose-defeating) to have to make a lot of changes at the lower C++ level to
| be able to pass and handle these SEXP types. Is this correct or is there
| something we missed?

I believe you missed something. You can pass entire objects (incl matrices)
down from R to C++ using Rcpp and they arrive as native C++ objects. You can
pass them back the same way.

So have a look at the vignette Rcpp-introduction included in the recent
releases (current is 0.7.12) as well as from the CRAN page as well as my
download page.

There are also numerous examples in the source package in the unit tests.
 
| (ii) From the convolution example (see slide #43/112 from Dirk?s recent talk
| ?Seamless R Extensions
| 
| using Rcpp and RInside? at UCLA on March 30th), it is necessary to replace the
| std::vector in C++ to an object of type RcppVector<double>. Do other  containers
| in C++ (set, list, map etc.) present in the original C++ code need to be
| replaced whenever they appear in the code, or is it just the containers which
| appear as arguments in the function call?

I am not sure if you read the whole document but RcppVector is part of the
so-called 'classic' API where what you desire (STL objects) is supported
rather well in the 'new' API.  Again, see the vignette.
 
| (iii) When including Rcpp.h and building a shared object, can problems arise if
| the version of the gcc compiler used to build the shared object is different
| from the gcc compiler version that was used to generate the binary for R from
| the source code?

In theory, yes or maybe. In practice, no. 

I have been doing this using shared libraries for Rcpp as well as
shared-library builds of R since 2005 or 2006, and I have not been bitten. In
practice one rebuilds every now and then anyway because of new features.

Hope this helps. Keep the follow-up questions coming.

-- 
  Regards, Dirk


More information about the Rcpp-devel mailing list