Search code examples
rrcpp

seq_len not recognized in rcpp


I'm new to Rcpp, and this may be a silly question, but I can't seem to get seq_len to work in rcpp, even though I know it should be an R-like function that Rcpp has. This is my code:

cppFunction("NumericVector foo2(int n){
IntegerVector x = Rcpp::seq_len(n);
return x;
            }")

This results in the error message: "error: no viable conversion from returned value of type 'Vector<13>' to function return type 'Vector<14>' return x;"


Solution

  • When I try your code, the compiler throws an error saying that an IntegerVector cannot be converted to a NumericVector in the output. However, a simple change to:

    Rcpp::cppFunction("IntegerVector foo2(int n){
    IntegerVector x = Rcpp::seq_len(n);
    return x;}")
    

    Produces no errors and we get:

    foo2(3)
    #> [1] 1 2 3