rcpp

C++ function not available

旧街凉风 提交于 2019-12-04 05:58:20
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 > high) acc = high; res[i] = acc; } return res; } I then Build & Reload and test out my new function.

Parameterisation of R::dexp

僤鯓⒐⒋嵵緔 提交于 2019-12-04 05:31:49
问题 I have just spent a while trying to find a bug in my code which has turned out to be an unusual (at least to me) parameterisation for the R::dexp function. For example: cppFunction(" double my_dexp(double x, double lambda, double is_log) { return R::dexp(x, lambda, is_log); } ") > my_dexp(4.5, 2.5, FALSE) [1] 0.06611956 > dexp(4.5, 2.5, FALSE) [1] 3.251824e-05 Looking here I can see that they use the definition: double R::dexp(double x, double sl, int lg) but I haven't been able to find out

Allow C++ constants to be a default function parameter using Rcpp Attributes

半世苍凉 提交于 2019-12-04 04:57:57
问题 I created a cumsum function in an R package with rcpp which will cumulatively sum a vector until it hits the user defined ceiling or floor. However, if one wants the cumsum to be bounded above, the user must still specify a floor. Example: a = c(1, 1, 1, 1, 1, 1, 1) If i wanted to cumsum a and have an upper bound of 3, I could cumsum_bounded(a, lower = 1, upper = 3) . I would rather not have to specify the lower bound. My code: #include <Rcpp.h> #include <float.h> #include <cmath> using

RStudio crashes with RCpp with reproducible codes

一笑奈何 提交于 2019-12-04 04:28:14
问题 @user3759195 wrote a post https://stackoverflow.com/questions/24322356/rstudio-crashes-and-it-does-not-reproduce about RStudio crashing with RCpp, but didn't give any reproducible case. @KevinUshey mentioned in the comments that we have to PROTECT the wrap within the code. I took the liberty of posting two alternatives to split.data.frame function written in RCpp: * VERSION THAT DOES NOT CRASH RSTUDIO * //[[Rcpp::export]] List splitDataFrameCpp(DataFrame x,NumericVector y) { int nRows=x.nrows

How to set up Eclipse + StatET + Rcpp on Windows

 ̄綄美尐妖づ 提交于 2019-12-04 04:17:14
When I came to know that I can create a R package with C++ using Rcpp, I was excited about it and eager to know development environment for it. And thanks to Fell Stat Blog , I could quickly establish a great environment using Eclipse with StatET, its plugin for R, to use Rcpp and RInside (another package for embedding R into your C++ application) on Windows. Since the blog was, however, based on OS X, several things required trial & error (& almost give-up) to adjust for Windows - it took me 6 hours of my leisure time. For example, you need to install Rtools to be able to compile C/C++ using

order a dataframe by column in Rcpp

霸气de小男生 提交于 2019-12-04 04:09:14
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, someColName2) // how to sort?? //// some code here } I would like to avoid accessing R's order function within

How create an Rcpp NumericVector with more than 20 entries?

假装没事ソ 提交于 2019-12-04 03:48:24
问题 Creating a NumericVector with more than 20 elements leads to error messages. This is in agreement with this document (at the very bottom): http://statr.me/rcpp-note/api/Vector_funs.html Currently, I expose a class (using RCPP_MODULE) of which one of its methods returns the desired NumericVector. How can I return more than 20 elements? #include <Rcpp.h> class nvt { public: nvt(int x, double y) {...} NumericVector run(void) { .... return NumericVector::create(_["a"]=1,_["b"]=2, .....,_["c"]=21)

Efficiency of matrix rowSums() vs. colSums() in R vs Rcpp vs Armadillo

六眼飞鱼酱① 提交于 2019-12-04 03:14:31
Background Coming from R programming, I'm in the process of expanding to compiled code in the form of C/C++ with Rcpp . As a hands on exercise on the effect of loop interchange (and just C/C++ in general), I implemented equivalents to R's rowSums() and colSums() functions for matrices with Rcpp (I know these exist as Rcpp sugar and in Armadillo -- this was just an exercise). Question I have my C++ implementation of rowSums() and colSums() along with Rcpp sugar and arma::sum() versions in this matsums.cpp file . Mine are just simple loops like this: NumericVector Cpp_colSums(const NumericMatrix

Fastest way to transpose a list in R / Rcpp

我与影子孤独终老i 提交于 2019-12-04 02:59:55
问题 I have a list: ls <- list(c("a", "b", "c"), c("1", "2", "3"), c("foo", "bar", "baz")) ls #> [[1]] #> [1] "a" "b" "c" #> [[2]] #> [1] "1" "2" "3" #> [[3]] #> [1] "foo" "bar" "baz" which I wish to "transpose" to give: resulting_ls #> [[1]] #> [1] "a" "1" "foo" #> [[2]] #> [1] "b" "2" "bar" #> [[3]] #> [1] "c" "3" "baz" I can achieve this with: mat <- matrix(unlist(ls), ncol = 3, byrow = TRUE) resulting_ls <- lapply(1:ncol(mat), function(i) mat[, i]) But with my real data it's very slow...(and I

R fast cbind matrix using Rcpp

和自甴很熟 提交于 2019-12-04 02:47:34
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 colnumber of first matrix int acoln=a.ncol(); //the colnumber of second matrix int bcoln=b.ncol(); /