Question

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

Was it helpful?

Solution

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

OTHER TIPS

You could try any one of these:

Other than that, yes, reading headers / source is a common fallback :)

The following simple program exhibits what rep, rep_each, and rep_len do and the differences between them.

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector rep_test(NumericVector vec, int kount) {
    return rep(vec, kount);
}

// [[Rcpp::export]]
NumericVector rep_each_test(NumericVector vec, int kount) {
    return rep_each(vec, kount);
}

// [[Rcpp::export]]
NumericVector rep_len_test(NumericVector vec, int kount) {
    return rep_len(vec, kount);
}

/*** R
vec = seq(-1, 1, length.out = 3)
vec
rep_test(vec, 4)
rep_each_test(vec, 4)
rep_len_test(vec, 4)
*/

Outputs are, respectively,

> vec
 [1] -1  0  1
> rep_test(vec, 4)
 [1] -1  0  1 -1  0  1 -1  0  1 -1  0  1
> rep_each_test(vec, 4)
 [1] -1 -1 -1 -1  0  0  0  0  1  1  1  1
> rep_len_test(vec, 4)
 [1] -1  0  1 -1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top