[Rcpp-devel] Rcpp::wrap segmentation fault

Dirk Eddelbuettel edd at debian.org
Sun Nov 21 14:24:22 CET 2010


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


More information about the Rcpp-devel mailing list