I'm currently working on an R package that uses Rcpp and RcppArmadillo and I have encountered a strange error that happens randomly. I can't post all the code of my functions to reproduce it from the scratch (it's 400 + 300 lines), but you can find the package on github: https://github.com/config-i1/CES - the function I use is in the "R/ces.R" file and it depends on the functions in "src/cesfun.cpp".
So when you install the package (devtools::install_github("config-i1/CES")
) and run the following commands:
library('Mcomp')
x <- cbind(c(rep(0,25),1,rep(0,43)),c(rep(0,10),1,rep(0,58)))
ces(ts(c(M3$N1457$x,M3$N1457$xx),frequency=12),h=18,holdout=T,intervals=T,seasonality="F",xreg=x,trace=T)->test
the error Error: not compatible with requested type
is sometimes returned. But this doesn't happen all the time (approximately 1 time out of 3 runs), so it is hard to trace this error. This thing happens on both Linux and Windows. And R just crashes on Mac OS instead of showing the error.
I actually have tried to conduct an investigation and found out that the error happens at random iteration in a for loop when the following Rcpp function is called...
code skipped
I know that I've probably done something wrong but I can't figure out what.
I would appreciate any help on this!
Thank you!
UPDATE.
I found what was the error in the code. So just in case someone encounters something similar, here's what was the problem and how to solve it:
I used one and the same function that was called both from R and Rcpp in different parts of the code. So the variables were defined as SEXP: forecaster(SEXP matxt, SEXP matF)
. As the result when I called the function in Rcpp I needed to use wrap()
to transform arma:mat
variables into SEXP
. The call was something like:
forecaster(wrap(matrixxt),wrap(matrixF))
.
In the majority of cases this worked perfectly well but sometimes for some reason wrap()
function was not doing what it was supposed to do and as the result something completely different was passed to forecaster
. The passed value in its' turn could not be transformed into NumericMatrix in forecaster
, thus the "Error: not compatible with requested type". This happened rarely and was hard to trace.
The solution was to change forecaster
arguments to the needed types: forecaster(arma::mat matrixxt, arma::mat matrixF)
, - pass the values to forecaster
in Rcpp directly, without the wrap()
, and write a special Rcpp wrapper function (so the forecaster
could be called from R).