[Rcpp-devel] Rcpp::wrap segmentation fault

Dirk Eddelbuettel edd at debian.org
Sun Nov 21 16:24:25 CET 2010


Hola Marc,

On 21 November 2010 at 15:45, marc michalewicz wrote:
| Thanks Dirk 
| 
| for this fast and useful reply. I tested your example and it works for me too, fine. But I am afraid I completely misunderstood the things - thx for being so kind to call it "almost understand":
| 
| Looking at things from the application architecture point of view: I have an existing and working C++ software (including a main of course) and want to use some R-functionality from this: e.g. using the lm()-functionality in R would be so convenient for doing some trendanalysis from the C++ program.

For the problem statement of 

    most of my logic is already in C++ but I want to easily 
    deploy some of the wizardry and functionality of R

RInside is a near-perfect fit.  Look at its webpage, source package and
examples, etc pp ...  
 
| So actually this seems not possible with the Rcpp package ? 

Yes: Rcpp is still used for the transfer back and forth between the R layer
and the C++ layer. Otherwise many of us have a problem statement that is

    we use R but there is the C++ library foo with 
    functions bar, baz and bam we want to use in R

where Rcpp makes it easy to access bar, baz and bam from R. That works via
the type of function I sent you an example of, and increasingly also with an
entirely new mechanism called 'Rcpp modules' that is inspired by Boost.Python.

You have to understand that our target audience is _R users_ who want to
extend R via C++ code.

Now, it so happens that I also had a need for the problem set above, and that
is how RInside came to be.  Rcpp then made it much more of a joy to use.

Hope this helps,  Dirk


| Is that right ? Sorry but the paper you mentioned I did not fully understand, though I tried to read it. Or would you suggest that I should read it more in depth for doing what I want ? It is just that you ar talking of a "Rcpp API" and I understand API that you include the header as the interface, link against library and in your main then you can use the functionality. But here it seems different.
| 
| Marc
| 
| 
| -------- Original-Nachricht --------
| > Datum: Sun, 21 Nov 2010 07:24:22 -0600
| > Von: Dirk Eddelbuettel <edd at debian.org>
| > An: "marc michalewicz" <marc.michalewicz at gmx.net>, rcpp-devel at lists.r-forge.r-project.org
| > Betreff: Re: [Rcpp-devel] Rcpp::wrap segmentation fault
| 
| > 
| > Marc,
| > 
| > On 21 November 2010 at 06:57, Dirk Eddelbuettel wrote:
| > | Basically, R itself is the main(). You never see that code. You simply
| > write
| > | functions all confirming to
| > 
| > Typo: "conforming" is what I meant.
| >  
| > |   SEXP myfunction(SEXP a, SEXP b, ...)
| > | 
| > | which take one or more SEXP objects and return one SEXP object.  You
| > call
| > 
| > Actually, zero, one, two, ... SEXP.
| > 
| > | this from as
| > | 
| > |   val <- .Call("myfunction", list(foo=1:3, bar="ABC"), cumsum(1:100))
| > | 
| > | which would supply two such arguments (the list and the vector).
| > | 
| > | Such 'myfunction' functions are now easier to write with Rcpp---as we
| > take of
| > | conversion from/to SEXP and also generally map the SEXP, the
| > representation
| > | of your R objects, to C++ objects. 
| > | 
| > | There are plenty of examples in the paper Romain and I wrote, here in
| > the
| > | list archives and at other places.  The "inline" package helps you do
| > all
| > | this at the R prompt meaning you do not need to call make, g++, ...
| > yourself.
| > 
| > As a concrete example, here is a slightly modified version of what you
| > sent.
| > No SEXP x needed, we return the STL object v instead:
| > 
| > -----------------------------------------------------------------------------
| > require(inline)
| > fun <- cxxfunction(signature(), '
| >   std::vector<std::map<std::string,int> > v;
| >   std::map<std::string, int> m1;             
| >   std::map<std::string, int> m2;             
| >   m1["foo"]=1; m1["bar"]=2;                  
| >   m2["foo"]=1; m2["bar"]=2; m2["baz"]=3;     
| >                                              
| >   v.push_back( m1 );                         
| >   v.push_back( m2 );                         
| >   return(Rcpp::wrap( v ));
| >   ',
| >   plugin="Rcpp")
| > -----------------------------------------------------------------------------
| > 
| > I can (automagically) paste this line by line from my editor to the R
| > process, and then call the function fun() it generates:
| > 
| > 
| > R> require(inline)
| > Loading required package: inline
| > R> fun <- cxxfunction(signature(), '
| > +   std::vector<std::map<std::string,int> > v;
| > +   std::map<std::string, int> m1;             
| > +   std::map<std::string, int> m2;             
| > +   m1["foo"]=1; m1["bar"]=2;                  
| > +   m2["foo"]=1; m2["bar"]=2; m2["baz"]=3;     
| > +   v.push_back( m1 );                         
| > +   v.push_back( m2 );                         
| > +   return(Rcpp::wrap( v ));
| > +   ',
| > +   plugin="Rcpp")                   
| > R> fun()
| > [[1]]
| > bar foo 
| >   2   1 
| > 
| > [[2]]
| > bar baz foo 
| >   2   3   1 
| > 
| > R> 
| > 
| > 
| > Hope this helps,  Dirk
| > 
| > -- 
| > Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
| 
| -- 
| GRATIS! Movie-FLAT mit über 300 Videos. 
| Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list