Reputation: 1722
I built a dummy Rcpp package using Rcpp.package.skeleton(myPackage)
with a single function:
// [[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:
Rcpp::sourceCpp('src/triple_balance.cpp')
triple_balance("10")
Error in triple_balance("10") : Not compatible with requested type: [type=character; target=double].
However, if I Install and Restart the package, running triple_balance("10")
crashes RStudio. I should add that the crash does not happen every time and it seems to depend on the initial state of the library, e.g. whether I installed previous versions of the package before, for example with or without const
in the function definition.
What could explain such an inconsistent behaviour?
My 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
Upvotes: 3
Views: 553
Reputation: 368181
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 triple_balance(const double& balance)
that is not what you supply when you type "10"
-- that is a character
vector (or "string")!
You can catch that autoMAGICally with Rcpp if you use vector arguments:
// [[Rcpp::export]]
NumericVector triple_balance(const NumericVector & balance) {
return balance * 3;
}
This now checks when the Rcpp data structure is instantiated:
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)
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.
Edit 2: For argument's sake, I also made it into a package, and it does not bring RStudio down either. I am running a very recent test version 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.
Upvotes: 5