Rcpp - how to check if any attribute is NULL

r, rcpp

Solution

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)

Problem

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`?

Original source

Related problems