Passing a `data.table` to c++ functions using `Rcpp` and/or `RcppArmadillo`

风流意气都作罢 提交于 2019-11-28 21:41:29

Building on top of other answers, here is some example code:

#include <Rcpp.h>
using namespace Rcpp ;

// [[Rcpp::export]]
double do_stuff_with_a_data_table(DataFrame df){
    CharacterVector x = df["x"] ;
    NumericVector   y = df["y"] ;
    IntegerVector   z = df["v"] ;

    /* do whatever with x, y, v */
    double res = sum(y) ;
    return res ;
}

So, as Matthew says, this treats the data.table as a data.frame (aka a Rcpp::DataFrame in Rcpp).

require(data.table)
DT <- data.table(
    x=rep(c("a","b","c"),each=3), 
    y=c(1,3,6), 
    v=1:9)
do_stuff_with_a_data_table( DT ) 
# [1] 30

This completely ignores the internals of the data.table.

Matt Dowle

Try passing the data.table as a DataFrame rather than NumericMatrix. It is a data.frame anyway, with the same structure, so you shouldn't need to convert it.

Dirk Eddelbuettel

Rcpp sits on top of native R types encoded as SEXP. This includes eg data.frame or matrix.

data.table is not native, it is an add-on. So someone who wants this (you?) has to write a converter, or provide funding for someone else to write one.

For reference, I think the good thing is to output a list from rcpp as data.table allow update via lists.

Here is a dummy example:

cCode <- 
    '
     DataFrame DT(DTi);
     NumericVector x = DT["x"];
     int N = x.size();
     LogicalVector b(N);
     NumericVector d(N);
     for(int i=0; i<N; i++){
         b[i] = x[i]<=4;
         d[i] = x[i]+1.;
     }
     return Rcpp::List::create(Rcpp::Named("b") = b, Rcpp::Named("d") = d);
    ';

require("data.table");
require("rcpp");
require("inline");
DT <- data.table(x=1:9,y=sample(letters,9)) #declare a data.table
modDataTable <- cxxfunction(signature(DTi="data.frame"), plugin="Rcpp", body=cCode)

DT_add <- modDataTable(DT)  #here we get the list
DT[, names(DT_add):=DT_add] #here we update by reference the data.table
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!