Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rep_each in Rcpp sugar

Tags:

r

rcpp

I started learning about Rcpp package few days ago and I'm gradually learning how to work with this package. I can see that for many functions in R, a corresponding function has already been written that works very similarly in C++ through the Rcpp package and I guess that is what referred as "Rcpp Sugar". I was trying to use something similar to rep() function(R) in my C++ code and I found that we have something called rep_each in Rcpp sugar:

I then found http://dirk.eddelbuettel.com/code/rcpp/html/classRcpp_1_1sugar_1_1Rep__each.html

The issue is after reading this page, I still have no idea how to use it. Even I don't know what the arguments are. Is there documentation that provides examples for different Rcpp sugar functions?

Thanks very much

like image 399
Sam Avatar asked Jan 19 '14 23:01

Sam


People also ask

What is RCPP sugar?

Rcpp sugar brings a higher-level of abstraction to C++ code written using the Rcpp API. Rcpp sugar is based on expression templates (Abrahams and Gurtovoy, 2004; Vandevoorde and Josuttis, 2003) and provides some 'syntactic sugar' facilities directly in Rcpp.


1 Answers

The Rep_each template class is an implementation detail. What you want to use is the rep_each function. For example:

#include <Rcpp.h>
using namespace Rcpp ;

// [[Rcpp::export]]
NumericVector rep_example( NumericVector x, int n){
  NumericVector res = rep_each(x, n) ;
  return res ;
}
like image 129
Romain Francois Avatar answered Sep 21 '22 00:09

Romain Francois