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.
printf("File %s does not exist",filename);
In C there are no subroutines, only functions, but functions are not required to return a value. The correct way to indicate that a function does not return a value is to use the return type "void". ( This is a way of explicitly saying that the function returns nothing. )
Character vectors and lists Each element of a STRSXP is a CHARSXP s, an immutable object that contains a pointer to C string stored in a global pool.
A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With