[英]Rcpp function crashes RStudio when packaged
I built a dummy Rcpp package using Rcpp.package.skeleton(myPackage)
with a single function:我使用
Rcpp.package.skeleton(myPackage)
和单个 function 构建了一个虚拟 Rcpp package:
// [[Rcpp::export]]
double triple_balance(const double& balance) {
if(R_IsNA(balance)) {
stop("balance is NA, please provide a numeric value");
} else {
double result = balance*3;
return result;
}
}
In unit tests, I want to make sure that the function returns an error when a wrong input is passed, which it consistently does if I source it:在单元测试中,我想确保 function 在传递错误输入时返回错误,如果我获取它,它会始终如一:
Rcpp::sourceCpp('src/triple_balance.cpp')
triple_balance("10")
Error in triple_balance("10"): Not compatible with requested type: [type=character;
Triple_balance("10") 中的错误:与请求的类型不兼容:[type=character; target=double].
目标=双]。
However, if I Install and Restart the package, running triple_balance("10")
crashes RStudio.但是,如果我安装并重新启动 package,运行
triple_balance("10")
会导致 RStudio 崩溃。 I should add that the crash does not happen every time and it seems to depend on the initial state of the library, eg whether I installed previous versions of the package before, for example with or without const
in the function definition.我应该补充一点,崩溃并非每次都发生,它似乎取决于库的初始 state,例如我之前是否安装了以前版本的 package,例如在 ZC1C425264E68384D1AB4A7 定义中是否有
const
。
What could explain such an inconsistent behaviour?什么可以解释这种不一致的行为?
My sessionInfo
:我的
sessionInfo
:
> sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux Server 7.8 (Maipo)
Matrix products: default
BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rcppDummy_1.0
loaded via a namespace (and not attached):
[1] compiler_3.6.0 tools_3.6.0 Rcpp_1.0.2 packrat_0.5.0
In the use case在用例中
Rcpp::sourceCpp('src/triple_balance.cpp')
triple_balance("10")
the error is yours because while your interface is using double
错误是你的,因为当你的界面使用
double
double triple_balance(const double& balance)
that is not what you supply when you type "10"
-- that is a character vector (or "string")!这不是您输入
"10"
时提供的内容——这是一个字符向量(或“字符串”)!
You can catch that autoMAGICally with Rcpp if you use vector arguments:如果你使用向量 arguments,你可以用 Rcpp 自动捕捉到它:
// [[Rcpp::export]]
NumericVector triple_balance(const NumericVector & balance) {
return balance * 3;
}
This now checks when the Rcpp data structure is instantiated:现在检查 Rcpp 数据结构何时被实例化:
R> triple_balance(10)
[1] 30
R> triple_balance(10L)
[1] 30
R> triple_balance("10")
Error in triple_balance("10") :
Not compatible with requested type: [type=character; target=double].
R>
Best of all, it works automagically on vectors:最重要的是,它自动在向量上工作:
R> triple_balance(c(c(2,3), seq(10,30,by=10), log10(1:3)))
[1] 6.00000 9.00000 30.00000 60.00000 90.00000 0.00000 0.90309 1.43136
R>
and also takes care of non-finite values:并且还处理非有限值:
R> triple_balance(c(10, NA, NaN, Inf, -Inf))
[1] 30 NA NaN Inf -Inf
R>
Edit By the way, if I keep your version in the source file with an _orig
appended, it works fine for me too (Ubuntu 20.04; current Rcpp)编辑顺便说一句,如果我将您的版本保留在源文件中并附加了
_orig
,它对我来说也可以正常工作(Ubuntu 20.04;当前 Rcpp)
R> triple_balance_orig("10")
Error in triple_balance_orig("10") :
Not compatible with requested type: [type=character; target=double].
R> packageVersion("Rcpp")
[1] ‘1.0.4.11’
R>
That behavior is not new code, so I am a little surprised it crashes your RHEL installation.这种行为不是新代码,所以我有点惊讶它会使您的 RHEL 安装崩溃。
Edit 2: For argument's sake, I also made it into a package, and it does not bring RStudio down either.编辑2:为了争论,我也把它变成了package,它也没有降低RStudio。 I am running a very recent test version 1.4.390.
我正在运行一个最近的测试版本 1.4.390。 (I have seen such crash on re-compilation of packages and errors, it can happen. It should not happen here.)
(我在重新编译包和错误时看到过这样的崩溃,它可能会发生。它不应该在这里发生。)
You could try your code in https://rstudio.cloud to quickly try a different enviroment.您可以在https://rstudio.cloud中尝试您的代码,以快速尝试不同的环境。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.