rcpp

Passing by reference a data.frame and updating it with rcpp

核能气质少年 提交于 2019-11-30 08:47:41
looking at the rcpp documentation and Rcpp::DataFrame in the gallery I realized that I didn't know how to modify a DataFrame by reference. Googling a bit I found this post on SO and this post on the archive. There is nothing obvious so I suspect I miss something big like "It is already the case because" or "it does not make sense because". I tried the following which compiled but the data.frame object passed to updateDFByRef in R stayed untouched #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] void updateDFByRef(DataFrame& df) { int N = df.nrows(); NumericVector newCol(N,1.); df[

passing unevaluated expressions to C/C++

不羁岁月 提交于 2019-11-30 08:00:34
问题 I'd like to pass a variable number of arguments from a function to C/C++, but would like to leave the arguments unevaluated and at the same time don't want to do any computations in R (aside from calling the C/C++ function), i.e. I don't want to call substitute in my R function. One option for this that I thought I could use is .External and doing smth like this: R_fn = function(...) .External("cpp_fn", ...) ... # and in C code: SEXP cpp_fn (SEXP arglist) { } However .External is evaluating

How to use Boost library in C++ with Rcpp

允我心安 提交于 2019-11-30 07:39:13
问题 I am using Rcpp package on R 3.0.0 . I am trying to run this example, but I cannot because I don't know how to use Boost . I installed Boost in the directory /Users/giorgi/boost_1_53_0 therefore I set Sys.setenv("PKG_CXXFLAGS"="-I /Users/giorgi/boost_1_53_0") but I am not sure I am doing the right thing. Sorry but I am quite ignorant with this stuff! 回答1: I would try a few things: Write a three line standalone C++ program using Boost, and compile it. This is just to prove to yourself that you

Requiring OpenMP availability for use in an Rcpp package

北战南征 提交于 2019-11-30 07:33:48
I have prepared a package in R by using RcppArmadillo and OpenMP libraries and following commands: RcppArmadillo.package.skeleton("mypackage") compileAttributes(verbose=TRUE) Also, in the DESCRIPTION file I added: Imports: Rcpp (>= 0.12.8), RcppArmadillo LinkingTo:Rcpp, RcppArmadillo Depends: RcppArmadillo and in the NAMESPACE file I added: import(RcppArmadillo) importFrom(Rcpp, evalCpp) Then I run the following codes in cmd : R CMD build mypackage R CMD INSTALL mypackage.tar.gz I build and install the package in my computer and I use it now. But my colleges and friends are not able to install

Matrix multiplication in Rcpp

戏子无情 提交于 2019-11-30 06:56:47
First of all, I am a novice user so forget my general ignorance. I am looking for a faster alternative to the %*% operator in R. Even though older posts suggest the use of RcppArmadillo, I have tried for 2 hours to make RcppArmadillo work without success. I always run into lexical issues that yield 'unexpected ...' errors. I have found the following function in Rcpp which I do can make work: library(Rcpp) func <- ' NumericMatrix mmult( NumericMatrix m , NumericMatrix v, bool byrow=true ) { if( ! m.nrow() == v.nrow() ) stop("Non-conformable arrays") ; if( ! m.ncol() == v.ncol() ) stop("Non

Row limit for data.table in R using fread

a 夏天 提交于 2019-11-30 05:04:20
I wanted to know if there is a limit to the number of rows that can be read using the data.table fread function. I am working with a table with 4 billion rows, 4 columns, about 40 GB. It appears that fread will read only the first ~ 840 million rows. It does not give any errors but returns to the R prompt as if it had read all the data ! I understand that fread is not for "prod use" at the moment, and wanted to find out if there was any timeframe for implementation of a prod-release. The reason I am using data.table is that, for files of such sizes, it is extremely efficient at processing the

Integrate Fortran, C++ with R

拥有回忆 提交于 2019-11-30 05:01:22
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::depends(RcppArmadillo)]] #include <RcppArmadillo.h> using namespace Rcpp; extern "C"{ List f_(int *n

Why does assert not work here?

我怕爱的太早我们不能终老 提交于 2019-11-30 04:59:23
问题 Here is the code: #include <Rcpp.h> #include <iostream> #include <assert.h> #include <stdio.h> using namespace Rcpp; // [[Rcpp::export]] double eudist(NumericVector x, NumericVector y) { int nx = x.size(); int ny = y.size(); std::cout << nx << '\n' << ny << std::endl; assert(nx == ny); double dist=0; for(int i = 0; i < nx; i++) { dist += pow(x[i] - y[i], 2); } return sqrt(dist); } After sourcing it into R, I get the following result, apparently it does not abort when there is an error: #/////

Ordering Permutation in Rcpp i.e. base::order()

不打扰是莪最后的温柔 提交于 2019-11-30 04:03:55
I have a ton of code using the base::order() command and I am really too lazy to code around that in rcpp. Since Rcpp only supports sort, but not order, I spent 2 minutes creating this function: // [[Rcpp::export]] Rcpp::NumericVector order_cpp(Rcpp::NumericVector invec){ int leng = invec.size(); NumericVector y = clone(invec); for(int i=0; i<leng; ++i){ y[sum(invec<invec[i])] = i+1; } return(y); } It somehow works. If the vectors are containing unique numbers, I get the same result as order(). If they are not unique, results are different, but not wrong (no unique solution really). Using it:

Convert RcppArmadillo vector to Rcpp vector

二次信任 提交于 2019-11-30 03:49:52
I am trying to convert RcppArmadillo vector (e.g. arma::colvec ) to a Rcpp vector ( NumericVector ). I know I can first convert arma::colvec to SEXP and then convert SEXP to NumericVector (e.g. as<NumericVector>(wrap(temp) ), assuming temp is an arma::colvec object). But what is a good way to do that? I want to do that simply because I am unsure if it is okay to pass arma::colvec object as a parameter to an Rcpp::Function object. I was trying to Evaluate a Rcpp::Function with argument arma::vec , it seems that it takes the argument in three forms without compilation errors. That is, if f is a