Skip to content
Prev 400 / 10988 Next

[Rcpp-devel] Handling NAs in RcppMatrix

The behavior I'm seeing is a bit bizarre.  The NaN conversion seems to
fail for RcppMatrix and RcppVector, but not for NumericVector.  The
most interesting bit is this: if I run the test code on e.g.
c(NA,1,2), RcppVector works (NA is printed as nan), whereas if I use
c(NA,1:2), instead of nan it prints a garbage value.

Here's the code I was using (just a bunch of Rprintfs, really):

RcppExport SEXP rcpp_test3(SEXP N1, SEXP V1, SEXP M1, SEXP parms) {

  SEXP rl=R_NilValue;
  char* exceptionMesg=NULL;

  try {

    RcppMatrix<double> m1(M1);// double mtx
    RcppVector<double> v1(V1);// double vector
    RcppParams rparam(parms);
    RcppResultSet rs;
    Rcpp::RObject n1sexp(N1);
    double n1 = n1sexp.asDouble();
    Rcpp::NumericVector nv1(V1);

    Rprintf("Printing number:\n%f\n",n1);
    Rprintf("Printing matrix:\n");
    for(int i=0; i < m1.rows(); i++){
      for(int j=0; j < m1.cols(); j++)
        Rprintf("(%d,%d):%f ",i,j,m1(i,j));
      Rprintf("\n");
    }
    Rprintf("Printing vector:\n");
    for(int i=0; i < v1.size(); i++)
      Rprintf("%f, ",v1(i));
    Rprintf("\n");
    Rprintf("Printing Numeric vector:\n");
    for(int i=0; i < nv1.size(); i++)
      Rprintf("%f, ",nv1(i));
    Rprintf("\n");

    rl = rs.getReturnList();

  } catch(std::exception& ex) {
    exceptionMesg = copyMessageToR(ex.what());
  } catch(...) {
    exceptionMesg = copyMessageToR("unknown reason");
  }

  if (exceptionMesg != NULL)
    Rf_error(exceptionMesg);

  return rl;
}
On Sat, Feb 6, 2010 at 11:19 PM, Dirk Eddelbuettel <edd at debian.org> wrote: