“错误:与请求的类型不兼容"随机发生 [英] "Error: not compatible with requested type" happening at random

查看:71
本文介绍了“错误:与请求的类型不兼容"随机发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个使用 Rcpp 和 RcppArmadillo 的 R 包,但我遇到了一个随机发生的奇怪错误.我无法发布我的函数的所有代码来从头开始复制它(它是 400 + 300 行),但是您可以在 github 上找到该包:https://github.com/config-i1/CES - 我使用的函数在R/ces.R"文件中,它依赖于"src/cesfun.cpp".

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".

所以当你安装包 (devtools::install_github("config-i1/CES")) 并运行以下命令时:

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

错误错误:与请求的类型不兼容有时会返回.但这不会一直发生(大约 3 次运行中的 1 次),因此很难跟踪此错误.这件事发生在 Linux 和 Windows 上.而 R 只是在 Mac OS 上崩溃,而不是显示错误.

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.

我实际上尝试进行了调查,发现在调用以下 Rcpp 函数时,错误发生在 for 循环中的随机迭代中...

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...

跳过代码

我知道我可能做错了什么,但我不知道是什么.

I know that I've probably done something wrong but I can't figure out what.

我将不胜感激!

谢谢!

更新.

我发现了代码中的错误.因此,以防万一有人遇到类似的问题,这里是问题所在以及如何解决它:

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:

我在代码的不同部分使用了一个从 R 和 Rcpp 调用的相同函数.所以变量被定义为 SEXP:forecaster(SEXP matxt, SEXP matF).结果,当我在 Rcpp 中调用该函数时,我需要使用 wrap()arma:mat 变量转换为 SEXP.电话是这样的:forecaster(wrap(matrixxt),wrap(matrixF)).

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)).

在大多数情况下,这工作得很好,但有时由于某种原因 wrap() 函数没有做它应该做的事情,结果一些完全不同的东西被传递给了 预报员.轮到传递的值无法在 forecaster 中转换为 NumericMatrix,因此出现错误:与请求的类型不兼容".这种情况很少发生,很难追踪.

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.

解决方案是将 forecaster 参数更改为所需类型:forecaster(arma::mat matrixxt, arma::mat matrixF), - 将值传递给 <直接在 Rcpp 中使用 code>forecaster,没有 wrap(),并编写一个特殊的 Rcpp 包装函数(因此 forecaster 可以从 R 中调用).

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).

推荐答案

欢迎使用 StackOverflow.环顾四周,感受一下哪些问题有帮助,哪些没有帮助.

Welcome to StackOverflow. Have a look around, and get a feeling for what questions are helpful and which ones aren't.

你的(目前)没有太大帮助——你只是在你的代码中有一个错误,每隔一段时间你就会提供一个不匹配的类型. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Rcpp 已经取得了一定的成功,在 CRAN 上有超过 450 个使用它的包,现在有 150 个使用 RcppArmadillo.因此,这些类型的对象已经被实例化了数百万次并代表了健壮的代码——所以你应该尝试进一步减少你的问题,直到你拥有 最小的可重现示例.

Yours is (at current) not helpful as too broad---you simply have a bug in your code where every once in a while you supply a non-matching type. Rcpp has become moderately successful with over 450 packages on CRAN using it and now 150 of those using RcppArmadillo. These types of objects have hence been instantiated millions of times and represent robust code -- so you should try to reduce your problem down further and further until you have the smallest possible reproducible example.

这篇关于“错误:与请求的类型不兼容"随机发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆