Reference to 'internal' is ambiguous when converting to matrix? - r

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.

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.

Related

Nesting Rcpp functions

Is it possible to nest Rcpp functions in each other?
I have a dummy example right here:
cppFunction(
'int add3(int x) {
return x+3;
}')
cppFunction(
'int add4(int x) {
return add3(x)+1;
}')
add3(2)
This does not work. How can I make this work?
Edit: Okay, so I followed Dirk's advice and now I have a test.cpp file:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int add3(int x) {
return x + 3;
}
// [[Rcpp::export]]
int add4(int x) {
return add3(x)+1;
}
Which I load in R with sourceCpp("test.cpp"). Now I can use both functions in R and they work although one function calls the other function.
The answers to your question get a little technical quickly, but are all provided in the Rcpp Attributes vignette, and have been for a long time.
First off, you are more-or-less misusing cppFunction(). It is made for quick and simple one-off function tests. Not for writing "infrastructure" or more complex code. For which you should use sourceCpp(), or better still, use a package.
If you switch you code to sourceCpp() and the [[Rcpp::export]] tag you will notice (in verbose=TRUE mode, or in package building) that the exported functions get 'transliterated' into other functions that R calls.
So yes you can nest functions, as you can in C / C++. Nothing is taken from you. But you cannot call the inner, nested function from R but that is possible with the API offered to us by R and which we use (and free you from interfacing directly). It only has SEXP .Call(functionanme, SEXP a, SEXP b,...) as an interface. I.e. a different signature.
But on the C / C++ you can nest, provided you compile your code differently.

Rcpp Armadillo package compilation "error: incorrect or unsupported type" when trying to convert a

I have the latest version of R, RCppArmadillo and RStudio installed. I started a brand new R project using Rcpp with Armadillo, and created a single new cpp file called rcpparma_basic.cpp.
This is the content of that file:
#include "RcppArmadillo.h"
// [[Rcpp::export]]
arma::rowvec rcpparma_head(arma::rowvec x, int n) {
Rcpp::IntegerVector first_rcpp = Rcpp::IntegerVector::create(0,1,2);
arma::uvec first(first_rcpp); // instantiate the armadillo vec from the Rcpp vec
arma::rowvec out = x.elem(first);
return out;
}
I thought it would be possible for me to convert the integer vector obtained from Rcpp into an arma vec, based on the example Dirk Eddelbuettel provided in his answer on this page:
How can I multiply an Armadillo matrix with a NumericVector obtained from qnorm()?
The reason I was trying to convert Rcpp IntegerVector to arma vec is that I wanted to be able to use the Rcpp seq function, and related functions, along with arma objects.
But when I try to compile the package, it fails with an error at this line in Mat_meat.h:
static_assert( is_same_type< eT, rcpp_type >::value , "error: incorrect or unsupported type" );
I am not suggesting the error is in Mat_meat.h, I'm sure I've got something wrong in my code, but based on that error message I have no idea what that mistake might be. So any help would be greatly appreciated!
Thanks
Louise
I've got a partial answer to what I was doing wrong: when I wrap the output of the Rcpp seq function, then I stop getting the Mat_meat.h error and get more informative errors.
#include "RcppArmadillo.h"
// [[Rcpp::export]]
arma::rowvec rcpparma_head(arma::rowvec x, int n) {
Rcpp::IntegerVector first_rcpp = Rcpp::IntegerVector::create(0,1,2);
arma::uvec first(Rcpp::wrap(first_rcpp)); // instantiate the armadillo vec from the Rcpp vec
arma::rowvec out = x.elem(first);
return out;
}
Then the errors I get are both for that wrap line and say "No matching function for call to arma::Col ::Col(SEXP)" and "invalid conversion from 'SEXP' {aka 'SEXPREC*'} to 'arma::uword' {aka 'unsigned int'} [-fpermissive]".
So at least now I'm getting more useful errors.
I was only trying to recreate a head() function in Rcpp as a mini practice function to clarify some of the arma/Rcpp rules for myself before moving on to a more complex function I need for my package. Given the fact that there seem to be more useful functions for me in Rcpp than in Armadillo, and the fact that the latest version of my algorithm no longer uses matrix algebra because the inner model doesn't allow it, I think I'll just stop using Armadillo.

RcppEigen: there are no arguments to 'assert' that depend on a template parameter

In trying to run one of the rcppeigen examples from the rcpp Gallery on Windows 10 I got the following error message:
/R/winlibrary/3.5/RcppEigen/include/unsupported/Eigen/src/MatrixFunctions/MatrixLogarithm.h:137:60:
error: there are no arguments to 'assert' that depend on a template
parameter, so a declaration of 'assert' must be available
[-fpermissive] assert(degree >= minPadeDegree && degree <=
maxPadeDegree);
I'm not sure what this means (I am very new to the Eigen library). The code was taken from the Rcpp Gallery:
#include <RcppEigen.h>
// [[Rcpp::depends(RcppEigen)]]
using Eigen::Map;
using Eigen::MatrixXd;
using Eigen::VectorXd;
using Eigen::SelfAdjointEigenSolver;
// [[Rcpp::export]]
VectorXd getEigenValues(Map<MatrixXd> M) {
SelfAdjointEigenSolver<MatrixXd> es(M);
return es.eigenvalues();
}
I used sourceCpp() to compile a file saved as TestEigen.cpp. I'd appreciate any information on what the issue may be. I looked for similar questions here but did not find a clear answer.
Please feel free to delete my post if this question is a duplicate.
Kind regards

variable scope in c++ function called through R with sourceCpp

I have one function nested inside another in R. Since the deeper one is a bit slow, I decided to use sourceCpp to swap in some compiled code. However, that inner function uses variables defined in the outer function. In R I use environments. What's the c++ analog? Do I have to use the extern keyword? Would something like this work?
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
arma::mat myFunc(arma::mat a, arma::mat b){
extern arma::mat c;
return a + b + c;
}
You can pass R environments down to C++ just fine via Rcpp; and examples exists in the docs, here, and in other places. Just randomly tossing a C++ extern in there, however, does access an environment.

Code auto-completion trouble with Rcpp and Netbeans/Eclipse

I'm trying to write some Rcpp code using Netbeans, primarily using the IDE for the method look-up / code completion. I've set up the project to include the R/include and R/library/Rcpp/include folders, and it seems this -almost- works. For example, if I write
#include <Rcpp.h>
Rcpp::CharacterVector x;
x. // hit CTRL+SPACE to pop-up methods available to x, no hits
However, if we look at what a CharacterVector is, we see:
typedef Vector<STRSXP> CharacterVector
which is defined in Rcpp/include/vector/instanstiation.h, so it should (?) just be inheriting all the methods available to the Rcpp::Vector class. Equivalently, if I write
Rcpp::Vector<STRSXP> x;
x. // hit CTRL+SPACE, and I do see a bunch of methods available
it does work. Furthermore, if I just copy the typedef declaration into my current source file, then the auto-completion does work.
So, I guess my question is - why does Netbeans struggle in finding the methods available to Rcpp::CharacterVector but not Rcpp::Vector?
Ultimately, it's not a big problem, but I am curious...

Resources