14

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?

Kun Ren
  • 4,715
  • 3
  • 35
  • 50

1 Answers1

22

Use this code:

return R_NilValue;

The same goes for C++ as well as C code; it's part of the R C API.

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
bartektartanus
  • 15,284
  • 6
  • 74
  • 102