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

Greg Minshall minshall at umich.edu
Sun Feb 24 15:55:37 CET 2013


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!

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
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;
}


More information about the Rcpp-devel mailing list