using C function from other package in Rcpp

Unfortunately cubature does not ship the headers in inst/include, so you have to borrow that from them and do something like this in your code: typedef void (*integrand) (unsigned ndim, const double *x, void *, unsigned fdim, double *fval); int adapt_integrate( unsigned fdim, integrand f, void *fdata, unsigned dim, const double *xmin, const double *xmax, … Read more

RcppArmadillo pass user-defined function

(Sometime you need to use svn log … on files to see how dated they are…) I think a better use case is in my “port” of the C-based DEoptim to Rcpp / RcppArmadillo: RcppDE. In it, I allow the optimization routine to use either an R function (as DEoptim does) or a user-supplied compiled … Read more

Replace negative values by zero

Thanks for the reproducible example. This is pretty basic R stuff. You can assign to selected elements of a vector (note an array has dimensions, and what you’ve given is a vector not an array): > pred_precipitation[pred_precipitation<0] <- 0 > pred_precipitation [1] 1.2091281 0.0000000 7.7665555 0.0000000 0.0000000 0.0000000 0.5151504 0.0000000 1.8281251 [10] 0.5098688 2.8370263 0.4895606 … Read more

Rcpp pass by reference vs. by value

They key is ‘proxy model’ — your xa really is the same memory location as your original object so you end up changing your original. If you don’t want that, you should do one thing: (deep) copy using the clone() method, or maybe explicit creation of a new object into which the altered object gets … Read more