C ++ function not available

I have the following cumsum_bounded.cpp file

#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;
}

Then I create and overwrite and test my new function.

cumsum_bounded(c(1, -2, 3), low = 2, high = 10)
[1] 1 0 3

Then I create the documentation. devtools::document()

When I Build & Reloadcompile everything fine.

But when I run cumsum_bounded(c(1, 2, 3), low= 2, high = 10), I get an 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 described above and DO NOT use the function Build & Reload, but devtools :: loadall (), it will work. But as soon as I press this button Build & Reload, it goes sideways.

+4
source share
1 answer

You probably need a line

useDynLib(<pkg>) ## substitute your package name for <pkg>

NAMESPACE. roxygen2, , . #' @useDynLib <pkg> - , <pkg> .

: , , - Rcpp, . @importFrom Rcpp evalCpp.

+10