Default NULL parameter Rcpp

家住魔仙堡 提交于 2019-12-01 03:38:15

You are in luck. We needed this in mvabund and Rblpapi, and have it since the last (two) Rcpp releases.

So try this:

// [[Rcpp::export]]
int test(int a, Rcpp::Nullable<Rcpp::IntegerVector> kfolds = R_NilValue) {

  if (kfolds.isNotNull()) {
     // ... your code here but note inverted test ...

A nice complete example is here in Rblpapi. You can also set a default value as you did (subject to the usual rules in C++ of all options to the right of this one also having defaults).

Edit: For completeness sake, here is a full example:

#include <Rcpp.h>

// [[Rcpp::export]]
int testfun(Rcpp::Nullable<Rcpp::IntegerVector> kfolds = R_NilValue) {

  if (kfolds.isNotNull()) {
    Rcpp::IntegerVector x(kfolds);
    Rcpp::Rcout << "Not NULL\n";
    Rcpp::Rcout << x << std::endl;
  } else {
    Rcpp::Rcout << "Is NULL\n";
  }
  return(42);
}

/*** R
testfun(NULL)
testfun(c(1L, 3L, 5L))
*/

which generates this output:

R> sourceCpp("/tmp/nick.cpp")

R> testfun(NULL)
Is NULL
[1] 42

R> testfun(c(1L, 3L, 5L))
Not NULL
1 3 5
[1] 42
R> 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!