Skip to content

[Rcpp-devel] [R] Error using Rcpp

2 messages · Romain Francois, Abhisek

#
Le 25/03/10 13:16, Abhisek a ?crit :
It is not. The Rcpp-devel mailing list is the right place:
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
You need some flags set up :

export PKG_LIBS=`Rscript -e "Rcpp:::LdFlags()"`
export PKG_CXXFLAGS=`Rscript -e "Rcpp:::CxxFlags()"`
R CMD SHLIB addone.cpp

also, your code is not correct since the i variable is not declared, so 
I get this when I try to compile it:

addone.cpp: In function ?SEXPREC* addone(SEXPREC*)?:
addone.cpp:12: error: ?i? was not declared in this scope
make: *** [addone.o] Error 1


The inline package can help you with this when prototyping your code, 
check the cfunction in the inline package, specifically its arguments 
"verbose" and "Rcpp".





Personally I would code this using stl algorithms rather than raw looping:

#include <Rcpp.h>

template <typename T>
inline T add_one( T x){
	return x + 1 ;
}

// This file takes in a vector and adds one to each entry
RcppExport SEXP addone(SEXP vec){

     // original object
     Rcpp::NumericVector orig(vec);

     // output vector
     Rcpp::NumericVector vecout(orig.size());

     std::transform(
     	orig.begin(), orig.end(),
     	vecout.begin(),
     	add_one<double> ) ;

     return vecout ;
}


or maybe you don't even have to write the tedious add_one template and 
you can do it like this:

#include <Rcpp.h>

// This file takes in a vector and adds one to each entry
RcppExport SEXP addone(SEXP vec){

     // original object
     Rcpp::NumericVector orig(vec);

     // output vector
     Rcpp::NumericVector vecout(orig.size());

     std::transform(
     	orig.begin(), orig.end(),
     	vecout.begin(),
     	std::bind2nd( std::plus<double>(), 1.0 )
     	) ;

     return vecout ;
}

  
    
#
Many thanks Romain!  It worked.  And thanks for pointing out the errors.

best
abhisek

On Thu, Mar 25, 2010 at 12:45 PM, Romain Francois
<romain at r-enthusiasts.com>wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20100325/fefcad15/attachment.htm