[Rcpp-devel] [R] Error using Rcpp

Romain Francois romain at r-enthusiasts.com
Thu Mar 25 13:45:19 CET 2010


Le 25/03/10 13:16, Abhisek a écrit :
>
> Hi, Im not sure if this is the right place to post this.

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

> I am using Xubuntu
> Karmic Koala and am trying to use the Rcpp package.  I am testing it using a
> simple code that takes in a vector and adds 1 to each element:
>
> #include<Rcpp.h>
>
> // This file takes in a vector and adds one to each entry
> RcppExport SEXP addone(SEXP vec){
>
>      // create a local copy of vec
>      Rcpp::NumericVector orig(vec);
>
>      // create an output vector
>      Rcpp::NumericVector vecout(orig.size());
>
>      for(i=0;i<orig.size();i++) {
>          vecout[i] = orig[i]+1;
>      }
>
>      Rcpp::Pairlist
> res(Rcpp::Named("result",vecout),Rcpp::Named("original",orig));
>
>      return res;
> }

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


> I then try to use - R CMD SHLIB addone.cpp
> that didnt work.  the error said there was no such file  - "Rcpp.h" .
> Then i came across Dirk Eddelbeutel's beamer presentation and followed the
> suggestion to copy Rcpp.h to /usr/loca/include and libRcpp.so to
> /usr/local/lib
> Then i tried R CMD SHLIB addone.cpp again but got many errors.  Here is a
> small selection:
>
> /usr/local/include/Rcpp.h:34:32: error: RcppDatetimeVector.h: No such file
> or directory
> /usr/local/include/Rcpp.h:35:23: error: RcppFrame.h: No such file or
> directory
> /usr/local/include/Rcpp.h:36:26: error: RcppFunction.h: No such file or
> directory
> /usr/local/include/Rcpp.h:37:22: error: RcppList.h: No such file or
> directory
>
> could someone help?  im afraid im new both to linux and Rcpp!
>
> best,
> abhisek


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/OIXN : raster images and RImageJ
|- http://tr.im/OcQe : Rcpp 0.7.7
`- http://tr.im/O1wO : highlight 0.1-5



More information about the Rcpp-devel mailing list