[Rcpp-devel] Alternative way of calling R functions within C++

2016-08-03 Thread George Vega Yon
Hey there, Looking at some old R code that I have I found this C++ function that allows evaluating R-written functions within C++ using Rcpp. While this is no news, the neat thing of it is that it seems to be faster than Rcpp::Function. Using microbenchmark I compared using Rcpp::function vs my im

Re: [Rcpp-devel] Alternative way of calling R functions within C++

2016-08-03 Thread Kevin Ushey
The problem with your implementation is what happens if R throws an error. The R longjmp will cause any C++ objects on the stack to leak, their destructors not to run, and you'll essentially be in a bad place if you evaluate any R code that might return an error. (For example, suppose you had a fil

Re: [Rcpp-devel] Alternative way of calling R functions within C++

2016-08-03 Thread Kevin Ushey
Also note that you definitely don't want to call `Rf_error` from a C++ context in general, as it will bypass any active C++ try-catch blocks, bypass destructors, and so on. You should call `Rcpp::stop` explicitly and use Rcpp attributes to ensure the try-catch block is automagically set up for you.

Re: [Rcpp-devel] Alternative way of calling R functions within C++

2016-08-03 Thread George Vega Yon
Thanks for the quick reply! What kind of errors are we talking about? I a new run I explicitly caused an error by passing a character vector, and had no memory leak (using Valgrind): cppFuncall(letters, fun) Error in cos(x[1]) : non-numeric argument to mathematical function If its not too much to

Re: [Rcpp-devel] Alternative way of calling R functions within C++

2016-08-03 Thread Dirk Eddelbuettel
On 3 August 2016 at 11:38, George Vega Yon wrote: | Thanks for the quick reply! What kind of errors are we talking about? I a new | run I explicitly caused an error by passing a character vector, and had no | memory leak (using Valgrind): | | cppFuncall(letters, fun) | Error in cos(x[1]) : non-nu

Re: [Rcpp-devel] Alternative way of calling R functions within C++

2016-08-03 Thread Kevin Ushey
The simplest demonstrating example I can think of: --- #include using namespace Rcpp; struct A { ~A() { Rprintf("~A()"); } }; // [[Rcpp::export]] void ouch() { A a; Rf_error("ouch!"); } /*** R ouch() */ --- Call 'Rcpp::sourceCpp()' on that and you'll see: > Rcpp::sourceCpp('~/Desktop/U

Re: [Rcpp-devel] Alternative way of calling R functions within C++

2016-08-03 Thread George Vega Yon
Dirk, good point. This is a very simple function and if I add complexity at the end there's no significant difference between calling -fun-, -cppFuncall- and -RcppFuncall-. Just to make this thread more complete, if I modify -fun- from fun <- function(x) { -cos(x[1])*cos(x[2])*exp(-((x[1] - pi)^