datatableAPI.h 中的 data.tables 与 Rcpp

huangapple go评论58阅读模式
英文:

data.tables with Rcpp (datatableAPI.h)

问题

R

name <- c("a", "a", "a", "a", "b")
value <- c(100, 200, 300, 400, 500)
group <- c("aa", "ab", "aa", "ab", "ab")
data <- data.table(name, value, group)

count <- 3

for(i in 1:count){
  data3 <- data[name == "a", .(mean_value = mean(value)), by = group]
}
data3

Rcpp

library(data.table)
Rcpp::cppFunction("SEXP analysis(SEXP data) { return dt::subsetDT(data[name == 'a', .(mean_value = mean(value)), by = group]); }", 
     include="#include <datatableAPI.h>", 
     depends="data.table")

name <- c("a", "a", "a", "a", "b")
value <- c(100, 200, 300, 400, 500)
group <- c("aa", "ab", "aa", "ab", "ab")
data <- data.table(name, value, group)
analysis(data)
英文:

I am new to Rcpp and learn to use Rcpp with data.table package in order to speed up the R code performance in R Markdown as I want to generate report. From https://github.com/Rdatatable/data.table/issues/4643, there is datatableAPI.h but I am not able to apply my own analysis to Rcpp with data.table. It produces errors as I tried to amend the following R code to suit my own analysis as I have tried a lot methods. I am able to run the following RCpp codes from https://github.com/Rdatatable/data.table/issues/4643:

RCpp

library(data.table)
dt &lt;- data.table(iris)
Rcpp::cppFunction(&quot;SEXP mysub2(SEXP x, SEXP rows, SEXP cols) { return dt::subsetDT(x,rows,cols); }&quot;, 
     include=&quot;#include &lt;datatableAPI.h&gt;&quot;, 
     depends=&quot;data.table&quot;)
mysub2(dt, 1:4, 1:4)

May I know how to transfer the R code below to Rccp code as shown above? Your help is appreciated.

R

name &lt;- c(&quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;b&quot;)
value &lt;- c(100, 200, 300, 400, 500)
group &lt;- c(&quot;aa&quot;, &quot;ab&quot;, &quot;aa&quot;, &quot;ab&quot;, &quot;ab&quot;)
data &lt;- data.table(name, value, group)

count &lt;- 3

for(i in 1: count){
  data3 &lt;- data[name == &quot;a&quot;, .(mean_value = mean(value)), by = group]
}
data3

The Rcpp code below is my current work. It shows error "
Error in sourceCpp(code = code, env = env, rebuild = rebuild, cacheDir = cacheDir, :
Error 1 occurred building shared library."

library(data.table)
Rcpp::cppFunction(&quot;SEXP analysis(SEXP data) { return dt::subsetDT(data[name == &#39;a&#39;, .(mean_value = mean(value)), by = group]); }&quot;, 
     include=&quot;#include &lt;datatableAPI.h&gt;&quot;, 
     depends=&quot;data.table&quot;)

name &lt;- c(&quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;b&quot;)
value &lt;- c(100, 200, 300, 400, 500)
group &lt;- c(&quot;aa&quot;, &quot;ab&quot;, &quot;aa&quot;, &quot;ab&quot;, &quot;ab&quot;)
data &lt;- data.table(name, value, group)
analysis(data)

Thank you.

答案1

得分: 2

欢迎来到 StackOverflow!我很高兴你找到了 data.table 的标题并使其工作 - 初步的步骤应该很容易!

但我们需要看看它提供了什么:

/* 为 C++ 使用添加一个命名空间 */
namespace dt {
  inline SEXP subsetDT(SEXP x, SEXP rows, SEXP cols) { 
        return DT_subsetDT(x, rows, cols); }
}

按行和列进行索引,就像你在示例中所做的一样。

现在看看你的示例:它并不匹配。你假设你可以像在 R 提示符中那样提交任何奇怪的 data.table 表达式。编译器告诉你你不能这样做。

可惜,编译器是对的。这基本上是我们在这里可以说的所有内容。

英文:

Welcome to StackOverflow! I am glad you found the header for data.table and got it work -- first steps are supposed to be easy!

But we need to look at what it offers:

/* add a namespace for C++ use */
namespace dt {
  inline SEXP subsetDT(SEXP x, SEXP rows, SEXP cols) { 
        return DT_subsetDT(x, rows, cols); }
}

Indexing by rows and columns. As you did in the example.

Now look at your example: it does not match. You assume you can submit any odd data.table expression as you do at the R prompt. The compiler tells you that you cannot.

Sadly, the compiler is right. And that is really all we can say here.

huangapple
  • 本文由 发表于 2023年3月31日 16:35:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定