Rcpp - how to check if any attribute is NULL

I have an Rcpp function that takes a digital matrix and returns a number vector. In my code, I have a line based on the recommendations of Kevin Ushi here: In Rcpp, how to return a vector with names that assigns NumericMatrix names to NumericVector names:

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

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

+6
source share
1 answer

Do 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, due to this awkwardness, we are likely to add rownames , colnames to Rcpp in the future.

(Checking the length is just to make sure there is something to find in index 1 the dimnames attribute)

+10
source

Source: https://habr.com/ru/post/972198/


All Articles