问题
I have the following file cumsum_bounded.cpp
#include <Rcpp.h>
using namespace Rcpp;
//' Cumulative sum.
//' @param x numeric vector
//' @param low lower bound
//' @param high upper bound
//' @param res bounded numeric vector
//' @export
//' @return bounded numeric vector
// [[Rcpp::export]]
NumericVector cumsum_bounded(NumericVector x, double low, double high) {
NumericVector res(x.size());
double acc = 0;
for (int i=0; i < x.size(); ++i) {
acc += x[i];
if (acc < low) acc = low;
else if (acc > high) acc = high;
res[i] = acc;
}
return res;
}
I then Build & Reload and test out my new function.
cumsum_bounded(c(1, -2, 3), low = 2, high = 10)
[1] 1 0 3
Then I build the documentation.
devtools::document()
When I Build & Reload
everything compiles fine.
But when I run cumsum_bounded(c(1, 2, 3), low= 2, high = 10)
I get the error:
Error in .Call("joshr_cumsum_bounded", PACKAGE = "joshr", x, low, high) :
"joshr_cumsum_bounded" not available for .Call() for package "joshr"
NAMESPACE
# Generated by roxygen2: do not edit by hand
export(cumsum_bounded)
Update:
If I create a new project as above and DON'T use the Build & Reload
function but rather devtools::loadall(), it will work. But once I press that Build & Reload
button, it goes sideways.
回答1:
You likely need the line
useDynLib(<pkg>) ## substitute your package name for <pkg>
in your NAMESPACE file. If you're using roxygen2, you can add a line e.g. #' @useDynLib <pkg>
somewhere in your documentation, substituting your package name for <pkg>
as appropriate.
EDIT: And in response to your other error message, you likely need to import something from Rcpp, e.g. add the line @importFrom Rcpp evalCpp
.
来源:https://stackoverflow.com/questions/36605955/c-function-not-available