[Rcpp-devel] error when formal argument name begins with an underscore

Dirk Eddelbuettel edd at debian.org
Sun Feb 24 16:21:10 CET 2013


Hi Greg,

On 24 February 2013 at 09:55, Greg Minshall wrote:
| hi.  i'm trying to understand the difference between using sourceCpp()
| and using R CMD compile/shlib.  (i seem to get different results using
| the different methods.)  if that's an FAQ, apologies, but any
| enlightenment would be appreciated!

The canonical reference for 'Rcpp attributes' is the vignette of the same
name.  

If the behaviour you seek is described there, good. Use it.  

If it is not described, then you have undefined behaviour :)  As you do here.

| experimenting, i ran into this problem that i haven't seen documented:
| if a formal argument to an [[Rcpp::export]] function *begins* with an
| underscore. sourceCpp() ends in failure.  with the example below,
| replacing "_parms" with "parms" makes sourceCpp() happy.  in the error

That seems to warrant two comments:

a) Patient: "Doctor, doctor, it hurts when I do it."
   Doctor:  "Then stop doing it."
   
   You have found the fix and workaround. What's not to like?

b) Looks like a possible bug at our end.  JJ is very diligent and will
   probably reply in due course, but it is after all the weekend...

   If you feel adventurous, you could poke into the source yourself.

Cheers, Dirk

| case, the message is:
| ----
| > sourceCpp(code=src)
| file15f7e455e0f6f.cpp: In function 'SEXPREC* newRcppMatrixExample(SEXPREC*)':
| file15f7e455e0f6f.cpp:40: error: expected `)' before '}' token
| file15f7e455e0f6f.cpp:40: error: expected ';' before '}' token
| make: *** [file15f7e455e0f6f.o] Error 1
| flag-sort -r g++ -I/sw/Library/Frameworks/R.framework/Versions/2.15/Resources/include -DNDEBUG  -I/sw/include  -I"/Users/minshall/Library/R/2.15/library/Rcpp/include"   -fPIC  -g -O2  -c file15f7e455e0f6f.cpp -o file15f7e455e0f6f.o 
| Error in sourceCpp(code = src) : 
|   Error 1 occurred building shared library.
| ----
| 
| cheers, Greg
| ----
| #include <algorithm>
| #include <Rcpp.h>
| 
| #include <cmath>
| 
| // [[Rcpp::export]]
| SEXP newRcppMatrixExample(SEXP _parms) {
|     Rcpp::List test(_parms);
|     try{ 
|         Rcpp::NumericMatrix orig(_parms);	// creates Rcpp matrix from SEXP
|         Rcpp::NumericMatrix mat(orig.nrow(), orig.ncol());	
| 
|         // we could query size via
|         //   int n = mat.nrow(), k=mat.ncol();
|         // and loop over the elements, but using the STL is so much nicer
|         // so we use a STL transform() algorithm on each element
|         std::transform(orig.begin(), orig.end(), mat.begin(), sqrt );
| 
|         return Rcpp::List::create(Rcpp::Named( "result" ) = mat, 
|                                   Rcpp::Named( "original" ) = orig);
|     } catch(std::exception& __ex__) {
|         forward_exception_to_r(__ex__);
|     } catch(...) {
|         ::Rf_error("c++ exception (unknown reason)");
|     }
|     return R_NilValue;
| }
| _______________________________________________
| 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

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


More information about the Rcpp-devel mailing list