Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ function not available

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.

like image 322
jwenzeslaus Avatar asked Apr 13 '16 17:04

jwenzeslaus


People also ask

Which of file function is not available in C language?

printf("File %s does not exist",filename);

Why is my function not returning a value in C?

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. )

What is CHARSXP?

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.

How do you declare a function in C?

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.


1 Answers

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.

like image 55
Kevin Ushey Avatar answered Oct 04 '22 14:10

Kevin Ushey