如何在Rcpp中向对象添加多个类?

时间:2013-11-14 16:00:44

标签: r rcpp

我试过这个:

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

它给了我一个错误:

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

1 个答案:

答案 0 :(得分:5)

也许是这样的:

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

可能存在通用append功能的空间。