0

I am trying to concert a dataframe to a matrix in rcpp as in this answer: best way to convert DataFrame to Matrix in RCpp

NumericMatrix testDFtoNM(DataFrame x) {
  NumericMatrix y = internal::convert_using_rfunction(x, "as.matrix");  
  return y;
}

and I get the error:

Line 135 reference to 'internal' is ambiguous.

This seems to indicate that there is more than one active namespace with internal as a definition.

How can I find the extra internal and/or how can I fix the code to compile without the error?

Update: Allan gave me the solution and using Rcpp::inline does allow it to work, but not why and how I can find the correct namespace on my own.

my current namespaces are:

using namespace Rcpp;
using namespace std;
using namespace arma;
using namespace RcppParallel;

Clearly internal exists in more than one of them. Is there a way to find which references have it without trial and error.

MichaelE
  • 763
  • 8
  • 22
  • 2
    Are you looking for `Rcpp::internal::convert_using_rfunction`? – Allan Cameron Aug 03 '20 at 16:53
  • Michael - the only safe way to do this is use full namespace identifiers. I use a fair bit of Rcpp, but tend to leave it for the interface part of porting larger C++ programs to R. Although I sometimes do `using namespace std;` in a cpp file, I always explicitly use full namespace identifiers in all other situations. – Allan Cameron Aug 03 '20 at 20:29

1 Answers1

4

The function you are trying to call is in the internal namespace which is nested inside the Rcpp namespace as you can see in the docs. Presumably you are using at least two namespaces with a nested namespace called internal. Therefore simply specifying

NumericMatrix testDFtoNM(DataFrame x) {
  NumericMatrix y = Rcpp::internal::convert_using_rfunction(x, "as.matrix");  
  return y;
}

should resolve the ambiguity.

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • 2
    Good answer, and let me add that piling on four different `using namespace X;` for four different values of `X` is almost _guaranteed_ to create that very problem. We do show it in examples because it make exposition and exploration easier, but if you look at package code you will see it is less frequently used there. – Dirk Eddelbuettel Aug 03 '20 at 18:17