Reputation: 33
I 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 <- data.table(iris)
Rcpp::cppFunction("SEXP mysub2(SEXP x, SEXP rows, SEXP cols) { return dt::subsetDT(x,rows,cols); }",
include="#include <datatableAPI.h>",
depends="data.table")
mysub2(dt, 1:4, 1:4)
How to transfer the R code below to Rccp code as shown above?
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
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("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)
Upvotes: 2
Views: 158