Search code examples
rrcpp

Return NA from Rcpp


I am trying to return NA via Rcpp. I do not understand why get_na() doesn't work here as suggested in this post?

> Rcpp::cppFunction('NumericVector temp1() {
   return NumericVector::get_na();
 }')
> temp1()
Error in temp1() : negative length vectors are not allowed

If I try create() it works.

> Rcpp::cppFunction('NumericVector temp2() {
    return NumericVector::create(NA_REAL);
  }')
> temp2()
  NA

Solution

  • You need to do something like:

    Rcpp::cppFunction('NumericVector temp1() {
      NumericVector y(1);
      y[0] = NumericVector::get_na();
      return y;
    }')
    
    temp1()
    #R> [1] NA
    

    if you want to use NumericVector::get_na(). Note that this member function simply returns NA_REAL which is why you presuambly get the error with NumericVector's constructor with:

    Rcpp::cppFunction('NumericVector temp1() {
       return NumericVector::get_na();
     }')
    

    You can equally well use NumericVector::create as you suggest. You can also do:

    Rcpp::cppFunction('NumericVector temp2() {
      return NumericVector(1, NA_REAL);
    }')
    

    or

    Rcpp::cppFunction('double temp3() {
      return NA_REAL;
    }')
    

    Return NA from Rcpp

    If you are dealing with other types of vectors then the NumericVector then the get_na function can be very useful. Here is an example where we return NA but with different types depending on the input.

    Rcpp::sourceCpp(code = '
      #include "Rcpp.h"
      using namespace Rcpp;
      
      template<int T>
      Vector<T> get_na_genric(){
        return Vector<T>(1, Vector<T>::get_na());
      }
      
      // [[Rcpp::export]]
      SEXP get_nan_vec(SEXP x) {
        switch (TYPEOF(x)) {
          case INTSXP : return get_na_genric<INTSXP >();
          case LGLSXP : return get_na_genric<LGLSXP >();
          case REALSXP: return get_na_genric<REALSXP>();
          case STRSXP : return get_na_genric<STRSXP >();
          case VECSXP : return get_na_genric<VECSXP >();
          stop("type not implemented");
        }
        
        return get_na_genric<REALSXP>();
      }')
    
    for(x in list(integer(), logical(), numeric(), character(), 
                  list())){
      out <- get_nan_vec(x)
      cat("got:\n")
      print(out)
      cat("with type ", typeof(out), "\n")
    }
    #R> got:
    #R> [1] NA
    #R> with type  integer 
    #R> got:
    #R> [1] NA
    #R> with type  logical 
    #R> got:
    #R> [1] NA
    #R> with type  double 
    #R> got:
    #R> [1] NA
    #R> with type  character 
    #R> got:
    #R> [[1]]
    #R> NULL
    #R> 
    #R> with type  list