[Rcpp-devel] Doubt about best pratices

Roberto Spadim roberto at spadim.com.br
Sat Apr 30 19:16:04 CEST 2022


Hi folks!

What's the best pratices to accept a dataframe and numericmatrix in a function?

Should I have two different methods to deal with NumericMatrix and DataFrame?
Could I convert DataFrame to matrix? What's the impact of doing this,
i.e. does the as.matrix copy dataframe data?

Thanks in advance!

---
Today I'm doing something like this:

void RCPP_function(const RObject &vec, const RObject &mat) {
  if(mat.isNULL())
    Rcpp::stop("mat can't be null");
  NumericMatrix variable_matrix;
  if (is<NumericMatrix>(mat))
    variable_matrix = as<NumericMatrix>(mat);
  else
    variable_matrix = Rcpp::internal::convert_using_rfunction(mat, "as.matrix");

  NumericVector variable_vector;
  if((is<NumericVector>(target) || is<DataFrame>(target)) && !target.isNULL()){
      variable_vector=as<NumericVector>(target);
  }else{
      variable_vector=colMeans(variable_matrix);
  }

  ...
  do something with data
  but here it's sure to be:
   NumericVector (variable_vector)
   NumericMatrix (variable_matrix)
  ...
}


-- 
Roberto Spadim


More information about the Rcpp-devel mailing list