rcpp

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

自古美人都是妖i 提交于 2019-12-06 05:51:07
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)); // evaluate and columnwise maximum entry of A const MatrixXd Amax(A.colwise().maxCoeff()); return

expose class in Rcpp - factory instead of constructor

萝らか妹 提交于 2019-12-06 04:48:41
There is a class I have to port to R. I have exposed it as a Rcpp module. However, I need to use that class outside of R also. I mean I want to test the class with google tests. In order to compile the tests the class must not use anything from Rcpp. Unfortunately, the function creating an instance of the class has to have quite flexible argument list. I would like to use named arguments since there will be many arguments you may choose to specify. For that reasons I cannot declare constructor that would be of any use within R. It would be ok, if I could define a function that would perform as

Passing R Function as Parameter to RCpp Function

本小妞迷上赌 提交于 2019-12-06 03:45:37
问题 I'm trying to run something like R my_r_function <- function(input_a) {return(input_a**3)} RunFunction(c(1,2,3), my_r_function) CPP #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] NumericVector RunFunction(NumericVector a, Function func) { NumericVector b = NumericVector(a.size()); for(int i=0; i<a.size(); i++) b[i] = func(a[i]); return b; } How would I make "Function func" actually work in Rcpp? P.S. I understand there are ways to do this without Rcpp (apply comes to mind for

Rcpp Copy a vector to a vector

喜欢而已 提交于 2019-12-06 03:17:15
问题 I am new to Rcpp and C++ coding in general, so forgive me for asking basic questions. The cpp part of the code is // test.cpp #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] void testRun() { IntegerVector Grp1 = IntegerVector::create(1,2,3,4); IntegerVector Grp2 = IntegerVector::create(3,4,5,6); Rf_PrintValue(Grp1); Grp1 = Grp2; Rf_PrintValue(Grp1); Grp2[3] = Grp2[3]+1; Rf_PrintValue(Grp1); } and when I run testrun(), I get the output > Rcpp::sourceCpp('test.cpp') > testRun() [1]

C++ function not available

匆匆过客 提交于 2019-12-06 01:58:17
问题 I have the following file cumsum_bounded.cpp #include <Rcpp.h> using namespace Rcpp; //' Cumulative sum. //' @param x numeric vector //' @param low lower bound //' @param high upper bound //' @param res bounded numeric vector //' @export //' @return bounded numeric vector // [[Rcpp::export]] NumericVector cumsum_bounded(NumericVector x, double low, double high) { NumericVector res(x.size()); double acc = 0; for (int i=0; i < x.size(); ++i) { acc += x[i]; if (acc < low) acc = low; else if (acc

Converting between NumericVector/Matrix and VectorXd/MatrixXd in Rcpp(Eigen) to perform Cholesky solve

冷暖自知 提交于 2019-12-06 00:25:26
Edit : with some clues from Dirk's answer below I worked this out, solution in the body of the question now. I'm sure this must be documented somewhere but my google skills are failing me. I'm developing an Rcpp package where I didn't think I would need dependency on Eigen, so I used NumericVector/Matrix quite extensively. However, I now need a Cholesky decomp/solve: I know how to do this using RcppEigen but I need VectorXd/MatrixXd 's. Say I have a P.S.D. NumericMatrix, A, and a NumericVector, b. I have tried various variations on the following: Rcpp::NumericVector b(bb); Rcpp::NumericMatrix

Rcpp quantile implementation

懵懂的女人 提交于 2019-12-05 22:20:49
I have a Rcpp double containing a NumericVector x. I would like to get the .95 quantile of such x within the Rcpp code flow. I do not know how can I get it. Is there an RcppArmadillo implementation? I'm not a Rcpp expert and my function probably needs lots of improvement, but it seems that you can easily create your own Rcpp quantile function (which will be not so accurate on small vectors due to high chance to skewness and problems with indexing on non-integer indexes, but improves as the vector grows) library(Rcpp) # You can use sourceCpp() instead of cppFunction if you wish cppFunction(

order a dataframe by column in Rcpp

橙三吉。 提交于 2019-12-05 21:56:55
问题 Is there any easy way to order a DataFrame by two (or more or one) of its columns within RCpp? There are many sorting algorithms available on the net, or I can use std::sort with a wrapper for DataFrame, but I was wondering if there is something already available within either RCpp or RCppArmadillo? I need to do this sorting / ordering as a part of another function DataFrame myFunc(DataFrame myDF, NumericVector x) { //// some code here DataFrame myDFsorted = sort (myDF, someColName1,

NA values in Rcpp conditional

允我心安 提交于 2019-12-05 20:59:47
问题 I am having trouble with conditionals in Rcpp. The best way to explain my problem is through an example. z <- seq(from=1,to=10,by=0.1) z[c(5,10,15,20,40,50,80)] <- NA src <- ' Rcpp::NumericVector vecz(z); for (int i=0;i<vecz.size();i++) { if (vecz[i] == NA_REAL) { std::cout << "Here is a missing value" << std::endl; } } ' func <- cxxfunction(signature(z="numeric"),src,plugin="Rcpp") func(z) # NULL From my understanding, NA_REAL represents the NA values for the reals in Rcpp and NA_Integer

R fast cbind matrix using Rcpp

一曲冷凌霜 提交于 2019-12-05 20:16:12
问题 cbind in R is relatively time consuming in repeated calls, but it also is powerful for various data types. I have written code that is 3X faster than cbind when binding two matrices. But bind_cols in dplyr package is merely 100X faster than cbind . It is only a pity that it cannot take matrix as input. Can someone make the code below more fast. Also, how do I fast bind sparse matrix? Here is the code I used: require( Rcpp ) func <- 'NumericMatrix mmult(NumericMatrix a,NumericMatrix b) { //the