[Rcpp-devel] Misunderstanding about sugar

Dirk Eddelbuettel edd at debian.org
Fri Oct 21 07:48:19 CEST 2011


On 20 October 2011 at 19:58, Michael Hannon wrote:
| Greetings.  I'm trying to bootstrap my way into Rcpp.  I have the impression
| that the "Sugar" extension to Rcpp allows one to use R syntax to run a
| substantial subset of R commands within C++ (i.e., using native C++ code, not
| calling back to R). 

That is largely correct.

The code is still somewhat new/experimental so not all pieces will guarantee
huge improvements. That said, the ones we have measured generally showed
great speed gains, and these examples are in our vignettes and other pieces/

| If that's the case, I evidently have a gaping hole in my
| understanding.

Is there a 'not' missing, or am I simply too tired to grok what you wrote?
 
| I tried to run the following R program, but I couldn't get the C++ part to
| compile:
| 
|     library(Rcpp)
|     library(inline)
|     
|     xorig <- c(1, -2, 3, -4, 5, -6, 7)
|       
|     code <- '
|         NumericVector x(xs);
|         return sapply( x, abs );
|     '

You may need '::abs' or '::fabs' as abs() exists in too many
places. Otherwise the idea seems right.

[ playing with it ... ]

You also nmeed to disentangle the assignment as there is too much template
magic.  So this just worked for me:


R> library(Rcpp)
R> library(inline)
R> 
R> xorig <- c(1, -2, 3, -4, 5, -6, 7)
R> 
R> code <- '
+     NumericVector x(xs);
+     NumericVector xa = sapply( x, ::fabs );
+     return(xa);
+     '
R> 
R> xabs <- cxxfunction(signature(xs="numeric"),
+                     plugin="Rcpp",
+                     body=code)
R> 
R> xabs(xorig)
[1] 1 2 3 4 5 6 7
R> 


Hth,  Dirk
       
|     xabs <- cxxfunction(signature(xs="numeric"),
|                         plugin="Rcpp",
|                         body=code)
|       
|     xabs(xorig)
|   
| I've appended the output of one of my attempts.  I'd appreciate it if somebody
| could point me in the right direction here.
| 
| -- Mike
| 
| 
| 
| $ R --vanilla < mhSugar.R
| 
| R version 2.13.1 (2011-07-08)
| Copyright (C) 2011 The R Foundation for Statistical Computing
| ISBN 3-900051-07-0
| Platform: x86_64-redhat-linux-gnu (64-bit)
| 
| R is free software and comes with ABSOLUTELY NO WARRANTY.
| You are welcome to redistribute it under certain conditions.
| Type 'license()' or 'licence()' for distribution details.
| 
|   Natural language support but running in an English locale
| 
| R is a collaborative project with many contributors.
| Type 'contributors()' for more information and
| 'citation()' on how to cite R or R packages in publications.
| 
| Type 'demo()' for some demos, 'help()' for on-line help, or
| 'help.start()' for an HTML browser interface to help.
| Type 'q()' to quit R.
| 
| > library(Rcpp)
| > library(inline)
| > 
| > xorig <- c(1, -2, 3, -4, 5, -6, 7)
| >   
| > code <- '
| +     NumericVector x(xs);
| +     return sapply( x, abs );
| + '
| >   
| > xabs <- cxxfunction(signature(xs="numeric"),
| +                     plugin="Rcpp",
| +                     body=code)
| file666a1f9c.cpp: In function ‘SEXPREC* file666a1f9c(SEXP)’:
| file666a1f9c.cpp:32:27: error: no matching function for call to
| ‘sapply(Rcpp::NumericVector&, <unresolved overloaded function type>)’
| file666a1f9c.cpp:32:27: note: candidate is:
| /usr/lib64/R/library/Rcpp/include/Rcpp/sugar/functions/sapply.h:103:1: note:
| template<int RTYPE, bool NA, class T, class Function>
| Rcpp::sugar::Sapply<RTYPE, NA, T, Function, Rcpp::traits::same_type<typename
| Rcpp::traits::result_of<Function>::type, typename
| Rcpp::traits::storage_type<Rcpp::traits::r_sexptype_traits<typename
| Rcpp::traits::result_of<Function>::type>::rtype>::type>::value>
| Rcpp::sapply(const Rcpp::VectorBase<RTYPE, LHS_NA, LHS_T>&, Function)
| make: *** [file666a1f9c.o] Error 1
| 
| ERROR(s) during compilation: source code errors or compiler configuration
| errors!
| 
| Program source:
|   1: 
|   2: // includes from the plugin
|   3: 
|   4: #include <Rcpp.h>
|   5: 
|   6: 
|   7: #ifndef BEGIN_RCPP
|   8: #define BEGIN_RCPP
|   9: #endif
|  10: 
|  11: #ifndef END_RCPP
|  12: #define END_RCPP
|  13: #endif
|  14: 
|  15: using namespace Rcpp;
|  16: 
|  17: 
|  18: // user includes
|  19: 
|  20: 
|  21: // declarations
|  22: extern "C" {
|  23: SEXP file666a1f9c( SEXP xs) ;
|  24: }
|  25: 
|  26: // definition
|  27: 
|  28: SEXP file666a1f9c( SEXP xs ){
|  29: BEGIN_RCPP
|  30: 
|  31:     NumericVector x(xs);
|  32:     return sapply( x, abs );
|  33: 
|  34: END_RCPP
|  35: }
|  36: 
|  37: 
| Error in compileCode(f, code, language = language, verbose = verbose) : 
|   Compilation ERROR, function(s)/method(s) not created! file666a1f9c.cpp: In
| function ‘SEXPREC* file666a1f9c(SEXP)’:
| file666a1f9c.cpp:32:27: error: no matching function for call to
| ‘sapply(Rcpp::NumericVector&, <unresolved overloaded function type>)’
| file666a1f9c.cpp:32:27: note: candidate is:
| /usr/lib64/R/library/Rcpp/include/Rcpp/sugar/functions/sapply.h:103:1: note:
| template<int RTYPE, bool NA, class T, class Function>
| Rcpp::sugar::Sapply<RTYPE, NA, T, Function, Rcpp::traits::same_type<typename
| Rcpp::traits::result_of<Function>::type, typename
| Rcpp::traits::storage_type<Rcpp::traits::r_sexptype_traits<typename
| Rcpp::traits::result_of<Function>::type>::rtype>::type>::value>
| Rcpp::sapply(const Rcpp::VectorBase<RTYPE, LHS_NA, LHS_T>&, Function)
| make: *** [file666a1f9c.o] Error 1
| Calls: cxxfunction -> compileCode
| In addition: Warning message:
| running command '/usr/lib64/R/bin/R CMD SHLIB file666a1f9c.cpp 2>
| file666a1f9c.cpp.err.txt' had status 1 
| Execution halted
| 
| 
| ----------------------------------------------------------------------
| _______________________________________________
| 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
-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the Rcpp-devel mailing list