0

I'm guessing this is an easy question, but I'm new to Cpp, and am stuck.

I've created a function in R, using Rcpp and:

// [[Rcpp::export]]

I can call the function in R and it works as intended. Let's call it F1().

Next, I want to create another function, F2(), in the same file cpp that calls the first function. I use standard function call language (i.e., F1(arguments)), and it compiles fine through R when I use sourceCpp(). But when I try to call F2() in R, I get:

Error in eval(expr, envir, enclos) : could not find function "F2"

Any advice?

Thanks.

#include <Rcpp.h>
using namespace Rcpp;


// [[Rcpp::export]]
NumericVector F1(NumericVector x) {
  return x * 2;
}
NumericVector F2(NumericVector x) {
    return x *F1(x);
}
 // even if i put return x*x*x instead of x*F1(X), I got the same error

/*** R
F1(42)
F2(51)
*/
coatless
  • 20,011
  • 13
  • 69
  • 84

1 Answers1

2

For each function you have to provide an exporter comment // [[Rcpp::export]]. This line is important to send the function into R. Your code must be written as:

#include <Rcpp.h>
using namespace Rcpp;


// [[Rcpp::export]]
NumericVector F1(NumericVector x) {
  return x * 2;
}
// [[Rcpp::export]]
NumericVector F2(NumericVector x) {
    return x *F1(x);
}


/*** R
F1(42)
F2(51)
*/
MACHERKI M E
  • 131
  • 9