It works, thanks a lot! Qiang Kou <q...@umail.iu.edu>于2016年11月11日 周五09:07写道:
> Hi, Jingyu, > > I post a small example below. I think this is what you want. > > Best, > > KK > > #include <Rcpp.h> > > using namespace Rcpp; > > //[[Rcpp::export]] > void test(Nullable<Function> f = R_NilValue) { > if (f.isNotNull()) { > Rcpp::Rcout << "Not NULL fun" << std::endl; > Function f2(f); > f2(); > } else { > Rcout << "NULL fun" << std::endl; > } > } > > /* R code > > Rcpp::sourceCpp("test.cpp") > > f_test <- function() { > + print("test fun") > + } > > test(f_test) > Not NULL fun > [1] "test fun" > > test() > NULL fun > */ > > > > > On Fri, Nov 11, 2016 at 7:18 AM, Dirk Eddelbuettel <e...@debian.org> wrote: > > > 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 | e...@debian.org > > _______________________________________________ > Rcpp-devel mailing list > Rcpp-devel@lists.r-forge.r-project.org > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel > > > > > -- > Qiang Kou > q...@umail.iu.edu > School of Informatics and Computing, Indiana University > >
_______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel