How to return R's NULL in Rcpp code?

2019-04-22 21:46发布

问题:

Suppose I have a C++ code to compile with Rcpp and will be called in R.

// [[Rcpp::export]]
SEXP to_env(List x) {
  if(x.hasAttribute("names"))
  {
    return x;
  }
  else
  {
    return NULL;
  }
}

What should the NULL value be to return R's NULL instead of a crash?

回答1:

Use this code:

return R_NilValue;


标签: r null rcpp