I would like to use this function (or just the String it returns):
const char* ERROR_TYPE() {
return "unknown type detected for big.matrix object!";
}
in my R(cpp) package.
I want to make it available to all my Rcpp functions (files in src/) and to all my tests (R files in tests/testthat/) .
In other terms, I would like to use throw Rcpp::exception(MESSAGE);
and testthat::expect_error(foo(), MESSAGE)
where MESSAGE is defined once.
I tried to read and test some of what is said in Rcpp Attributes but it doesn't seem to work for my problem.
The first thing I've tried is to define
// [[Rcpp::export]]
const char* ERROR_TYPE() {
return "unknown type detected for big.matrix object!";
}
but it doesn't scope to other Rcpp files.
Then, I tried to #include "myfile.cpp"
in others Rcpp files but I had multiple defines, even when trying to use inline
or #ifndef #define #endif
but I think it's odd for an C++ file.
Finally, I tried to use a inst/include/mypackage.h
and define my function or my variable there but it didn't seem to scope to other C++ functions as well.
One trick seems to work, define an R function
ERROR_TYPE <- function() {
"unknown type detected for big.matrix object!"
}
and then use
Function err("ERROR_TYPE");
throw Rcpp::exception(as<const char*>(err()));
in your Rcpp functions.
Doesn't seem to be good practice though.
And, it works with devtools::test()
but not devtools::check()
or Travis-CI (can't find the function) so it is not a solution as well.