Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple classes to an object in Rcpp?

Tags:

r

rcpp

I have tried this:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector difflag(NumericVector x, int lag) {
    int n = x.size();
    NumericVector out(n-lag);

    for(int i=0; i<(n-lag); i++) {
        out[i] = x[i+lag] - x[i];
    }

    out.attr("class") += "myclass";
    return out;
}

It gives me an error:

all.cpp: In function ‘Rcpp::NumericVector difflag(Rcpp::NumericVector, int)’:
all.cpp:64:26: error: no match for ‘operator+=’ in ‘Rcpp::RObject::attr(const string&) const((* & std::basic_string<char>(((const char*)"class"), (*(const std::allocator<char>*)(& std::allocator<char>()))))) += "myclass"’
make: *** [all.o] Error 1
like image 934
qed Avatar asked Nov 14 '13 16:11

qed


1 Answers

Perhaps something like :

 CharacterVector classes = out.attr( "class" ) ;
 classes.push_back( "myclass" ) ;
 out.attr( "class" ) = classes ;

There could be room for a generic append function.

like image 105
Romain Francois Avatar answered Oct 13 '22 12:10

Romain Francois