rcpp

What are productive ways to debug Rcpp compiled code loaded in R (on OS X Mavericks)?

回眸只為那壹抹淺笑 提交于 2019-11-29 01:05:00
What is the most productive and quickest way to debug shared objects that are loaded into R, in particular on OS X Mavericks? I'm primarily interested in debugging compiled Rcpp code. I have read the R externals on debugging compiled code ( http://cran.r-project.org/doc/manuals/R-exts.html#Debugging-compiled-code ) which favours using gdb, but gdb isn't officially supported on Mavericks. However, it seems that lldb is a viable alternative? I found the most useful resource for working out how to debug compiled code in R from Dirk's response to this post (Thanks Dirk!) ( Debugging (line by line)

Integrate Fortran, C++ with R

陌路散爱 提交于 2019-11-29 00:54:07
问题 My task it to rewrite a R function in C++ to accelerate the while loops. All R codes has been rewritten in the help of Rcpp and Armadillo except the .Fortran() . I try to use Rinside to at first and it works at a very slow speed as Dirk indicated. (It is expensive for data to go through R -> C++ -> R -> Fortran) Since I don't want to rewrite the Fortran codes in C++ and vice versa, it looks natural to accelerate the programs by linking C++ directly to Fortran: R -> C++ -> Fortran. // [[Rcpp:

Moving from sourceCpp to a package w/Rcpp

回眸只為那壹抹淺笑 提交于 2019-11-29 00:41:30
问题 I currently have a .cpp file that I can compile using sourceCpp() . As expected the corresponding R function is created and the code works as expected. Here it is: #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] NumericVector exampleOne(NumericVector vectorOne, NumericVector vectorTwo){ NumericVector outputVector = vectorOne + vectorTwo; return outputVector; } I am now converting my project over to a package using Rcpp . So I created the skeleton with rStudio and started looking

Declare a variable as a reference in Rcpp

怎甘沉沦 提交于 2019-11-29 00:41:20
In C++, we can declare a variable as a reference. int a = 10; int& b = a; If we set b=15 , a also changes. I want to do the similar thing in Rcpp. List X = obj_from_R["X"]; IntegerVector x_i = X[index]; x_i = value; I want to update an object from R called X by inserting a value to one of its vector. The code above did not work, so I tried this: IntegerVector& x_i = X[index]; and received an error. error: non-const lvalue reference to type 'IntegerVector' (aka 'Vector<13>') cannot bind to a temporary of type 'Proxy' (aka 'generic_proxy<19>') coatless This question gets asked a lot in differing

How to build a R package which use Rcpp with external c++ libraries?

ε祈祈猫儿з 提交于 2019-11-29 00:09:59
Such as boost, where can I specify the following: 1.External c++ header file include path 2.External c++ source file 3.External c++ link library file path It all goes into src/Makevars as explained in the fine manual Writing R Extensions that came with R either the Writing a package using Rcpp vignette or my book both of which I told you about in ... ... my replies to your post on rcpp-devel 来源: https://stackoverflow.com/questions/18570526/how-to-build-a-r-package-which-use-rcpp-with-external-c-libraries

Index element from list in Rcpp

自作多情 提交于 2019-11-28 23:55:19
Suppose I have a List in Rcpp, here called x containing matrices. I can extract one of the elements using x[0] or something. However, how do I extract a specific element of that matrix? My first thought was x[0](0,0) but that does not seem to work. I tried using * signs but also doesn't work. Here is some example code that prints the matrix (shows matrix can easily be extracted): library("Rcpp") cppFunction( includes = ' NumericMatrix RandMat(int nrow, int ncol) { int N = nrow * ncol; NumericMatrix Res(nrow,ncol); NumericVector Rands = runif(N); for (int i = 0; i < N; i++) { Res[i] = Rands[i];

How to handle list in R to Rcpp

拜拜、爱过 提交于 2019-11-28 23:40:22
I have a list in R that x<-list(c(1,2,3), c(4,5), c(5,5), c(6)). I want to input the list to Rcpp and return them as an average vector, c(2, 4.5, 5, 6). I am not sure how to handle the list in Rcpp. I got an error message, so could someone check my code? library(inline) fx = cxxfunction(signature(x='List'), body = ' Rcpp::List xlist(x); int n = xlist.size(); double res[n]; for(int i=0; i<n; i++) { Rcpp NumericVector y(xlist[i]); int m=y.size(); res[i]=0; for(int j=0; j<m; j++){ res[i]=res[i]+y[j] } } return(wrap(res)); ' , plugin='Rcpp') x<-list(c(1,2,3), c(4,5), c(5,5), c(6)) fx(x) A couple

Passing a `data.table` to c++ functions using `Rcpp` and/or `RcppArmadillo`

风流意气都作罢 提交于 2019-11-28 21:41:29
Is there a way to pass a data.table objects to c++ functions using Rcpp and/or RcppArmadillo without manually transforming to data.table to a data.frame ? In the example below test_rcpp(X2) and test_arma(X2) both fail with c++ exception (unknown reason) . R code X=data.frame(c(1:100),c(1:100)) X2=data.table(X) test_rcpp(X) test_rcpp(X2) test_arma(X) test_arma(X2) c++ functions NumericMatrix test_rcpp(NumericMatrix X) { return(X); } mat test_arma(mat X) { return(X); } Building on top of other answers, here is some example code: #include <Rcpp.h> using namespace Rcpp ; // [[Rcpp::export]] double

Should I prefer Rcpp::NumericVector over std::vector?

橙三吉。 提交于 2019-11-28 21:38:43
Is there any reason why I should prefer Rcpp::NumericVector over std::vector<double> ? For example, the two functions below // [[Rcpp::export]] Rcpp::NumericVector foo(const Rcpp::NumericVector& x) { Rcpp::NumericVector tmp(x.length()); for (int i = 0; i < x.length(); i++) tmp[i] = x[i] + 1.0; return tmp; } // [[Rcpp::export]] std::vector<double> bar(const std::vector<double>& x) { std::vector<double> tmp(x.size()); for (int i = 0; i < x.size(); i++) tmp[i] = x[i] + 1.0; return tmp; } Are equivalent when considering their working and benchmarked performance. I understand that Rcpp offers sugar

Why is this naive matrix multiplication faster than base R's?

馋奶兔 提交于 2019-11-28 21:09:03
In R, matrix multiplication is very optimized, i.e. is really just a call to BLAS/LAPACK. However, I'm surprised this very naive C++ code for matrix-vector multiplication seems reliably 30% faster. library(Rcpp) # Simple C++ code for matrix multiplication mm_code = "NumericVector my_mm(NumericMatrix m, NumericVector v){ int nRow = m.rows(); int nCol = m.cols(); NumericVector ans(nRow); double v_j; for(int j = 0; j < nCol; j++){ v_j = v[j]; for(int i = 0; i < nRow; i++){ ans[i] += m(i,j) * v_j; } } return(ans); } " # Compiling my_mm = cppFunction(code = mm_code) # Simulating data to use nRow =