[Rcpp-devel] Rcpp::wrap segmentation fault

marc michalewicz marc.michalewicz at gmx.net
Sun Nov 21 15:45:43 CET 2010


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.

So actually this seems not possible with the Rcpp package ? 
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


More information about the Rcpp-devel mailing list