[Rcpp-devel] How to set a default value when call a R function in Rcpp

Dirk Eddelbuettel edd at debian.org
Fri Nov 11 13:18:56 CET 2016


On 11 November 2016 at 05:53, Jingyu He wrote:
| I've deleted the post on StackOverflow. Sorry for the ambiguity, the problem is
| not arg, but the Function f. 
| 
| 
| #include <Rcpp.h>
| using namespace Rcpp;
| // [[Rcpp::export]]
| RObject cppfunction(Function rfunction = NULL) {
|       if( rfunction == NULL){
|       cout << 1 + 1 << endl;  // if we don't pass rfunction to cppfunction, run
| something not related to rfunction() 
|       }else{
|      rfunction()  // if we pass rfunction to cppfunction, run it
|       }
| }
| 
| rfunction is an argument of cppfunction. Sometimes I need to pass rfunction to
| cppfunction sometimes not. So I need to set a default rfunction (something like
| NULL indicating not passing it). 
| 
| But 
|    RObject cppfunction(Function rfunction = NULL) 
| doesn't work. How can I make it?

Well that is a different question :) But it has a nice answer: use

   Rcpp::Nullable<Rcpp::Function> rfunction

and test via

   if (rfunction.isNull())

(Untested, but should work)

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list