rcpp

How can I access the levels of the factors of a data.frame in Rcpp when it is passed as an argument from R?

◇◆丶佛笑我妖孽 提交于 2019-12-10 18:52:10
问题 I have the following data.frame df1 with factor fac1 that I have created in R and I pass it as an argument in a Rcpp function. The requested task is to get all the factor levels inside an Rcpp function and use them. fac1 <- factor(x = sample(x = 1:5,size = 100,replace = T),labels = paste0("D",1:5)) var1 <- sample(x = 1:20,size = 100,replace = T) fac2 <- factor(x = sample(x = 1:12,size = 100,replace = T),labels = paste0("M",1:12)) df1 <- data.frame(fac1,var1,fac2) 回答1: After some research I

Using a class as a parameter in a constructor of another class

…衆ロ難τιáo~ 提交于 2019-12-10 18:51:47
问题 I read this blog post about classes and modules in Rcpp and I tried to recreate it, but I am having trouble. Here is an abbreviated version of the code from the post: #include <Rcpp.h> using namespace Rcpp; class Point { public: Point( double x_, double y_) : x(x_), y(y_){} double x, y ; } ; class Shape { public: Shape( const Point& center_ ) : center(center_){} Point center ; virtual double area() const { return 0.0 ;} virtual bool contains(const Point& point) const { return false ; } } ;

Rcpp Function slower than Rf_eval

佐手、 提交于 2019-12-10 18:38:10
问题 I have been working on a package that uses Rcpp to apply arbitrary R code over a group of large medical imaging files. I noticed that my Rcpp implementation is considerably slower than the original pure C version. I traced the difference to calling a function via Function, vs the original Rf_eval. My question is why is there a close to 4x performance degradation, and is there a way to speed up the function call to be closer in performance to Rf_eval? Example: library(Rcpp) library(inline)

Calling R function from C++ on Windows

一个人想着一个人 提交于 2019-12-10 17:49:47
问题 I am trying to call R functions from C++ on Windows. I am using MinGW for compiling the program, but it throws error while compiling. Code (taken from Dirk) and compilation error are as follows: #include <iostream> using namespace std; #include "RInside.h" // for the embedded R via RInside Rcpp::NumericMatrix createMatrix(const int n) { Rcpp::NumericMatrix M(n,n); for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { M(i,j) = i*10+j; } } return(M); } int main(int argc, char *argv[]) { const int

Passing R functions to C routines using rcpp

試著忘記壹切 提交于 2019-12-10 16:49:09
问题 I have a C function from a down-stream library that I call in C like this result = cfunction(input_function) input_function is a callback that needs to have the following structure double input_function(const double &x) { return(x*x); } Where x*x is a user-defined computation that is usually much more complicated. I'd like to wrap cfunction using Rcpp so that the R user could call it on arbitrary R functions. NumericVector rfunction(Function F){ NumericVector result(1); // MAGIC THAT I DON'T

Serialization for Rcpp exposed class

末鹿安然 提交于 2019-12-10 16:46:37
问题 I've written a C++ class in an R package that I expose to the R namespace with RCPP_EXPOSED_CLASS and RCPP_MODULE . Everything works great: > index An object of class "Index" Slot "index": C++ object <0x9cd4810> of class 'DB' <0xfd66220> but if saveRDS(index, 'DB.rds') it does not save the actual object just the address. In turn it is casted as invalid when I load the DB.rds in a new session. Is it possible to write a custom serialization method that could work transparently with saveRDS ? 来源

Unexpected result from fibonacci series using Rcpp

╄→гoц情女王★ 提交于 2019-12-10 15:42:37
问题 I'm just starting to use Rcpp so sorry if I'm missing an easy step or something similar... I have tried this from ?sourceCpp library(Rcpp) sourceCpp(code=' #include <Rcpp.h> // [[Rcpp::export]] int fibonacci(const int x) { if (x == 0) return(0); if (x == 1) return(1); return (fibonacci(x - 1)) + fibonacci(x - 2); }' ) Up to fibonacci(46) everything's fine, but then I get: > fibonacci(47) [1] -1323752223 > fibonacci(48) [1] 512559680 > fibonacci(49) [1] -811192543 > fibonacci(50) [1]

Using colon (':') to access elements in an array in C++ (in Rcpp)

佐手、 提交于 2019-12-10 14:42:25
问题 I am trying to run the following code. Frankly I know C++ only little but I want to get the following function run. Can you help me run this silly example? cppFunction( 'NumericVector abc(int x, int x_end, NumericVector y) { NumericVector z; int x1 = x + x_end; z = y[x:x1]; return(z); }' ) abc(3,c(0,1,10,100,1000,10000)) I see this ... error: expected ']' before ':' token Update Sorry I forgot to mention that I need to generate a sequence of numbers from x to x1 . The function IntegerVector:

parallelize Own Package in R

这一生的挚爱 提交于 2019-12-10 11:44:13
问题 As recommended in other posts I wrote my own package in R to parallelize functions I wrote with Rcpp. I can load the package and everything works, but when I'm using optimParallel, I get the message: Error in checkForRemoteErrors(val) : 3 nodes produced errors; first error: object '_EffES_profileLLcpp' not found Here is what I'm doing: library(optimParallel) library(EffES) # EffES is my own package cl <- makeCluster(detectCores()-1) clusterEvalQ(cl, library(EffES)) clusterEvalQ(cl, library

RcppEigen - going from inline to a .cpp function in a package and “Map”

我只是一个虾纸丫 提交于 2019-12-10 11:28:16
问题 Everything seems to work in my package, but I wanted to check if the steps to make it were correct and about memory use using "Map". (It's a simple example, somewhere in-between the inline examples and the fastLm() example.) Here is an inline function that takes the maximum over each column of a matrix: library(Rcpp); library(inline); library(RcppEigen); maxOverColCpp <- ' using Eigen::Map; using Eigen::MatrixXd; // Map the double matrix AA from R const Map<MatrixXd> A(as<Map<MatrixXd> >(AA))