Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rcpp - how to check if any attribute is NULL

Tags:

r

rcpp

I have a Rcpp function that accepts a NumericMatrix and returns a NumericVector. In my code I have a line based on recommendations by Kevin Ushey here: In Rcpp - how to return a vector with names that assigns column names of the NumericMatrix to the NumericVector :

out.attr("names")=VECTOR_ELT(inp.attr("dimnames"),1)

This code gives an error if the NumericMatrix didn't have column names, i..e colnames(inp)=NULL. How can I check in Rcpp if the VECTOR_ELT(inp.attr("dimnames"),1) is NULL?

like image 981
uday Avatar asked Jul 14 '14 19:07

uday


1 Answers

You want Rf_isNull:

SEXP dm = inp.attr("dimnames");
if (!Rf_isNull(dm) && Rf_length(dm) > 1) {
    out.attr("names") = VECTOR_ELT(dm, 1);
}

FWIW, because of this awkwardness we will likely be adding rownames, colnames convenience methods to Rcpp sometime in the future.

(The length check is just to make sure that there is actually something to find at index 1 of the dimnames attribute)

like image 98
Kevin Ushey Avatar answered Nov 15 '22 22:11

Kevin Ushey