18
votes

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, using Rcpp which 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 .Primitive(".Call")(

and

F2 is missing

The first .cpp file contains

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double F1(NumericVector a) {
  int n = a.size();
  double result=0;  // create output vector
  double ss = 0;

  for(int i = 0; i < n; ++i) {
    ss += pow(a[i],2);
  }

  result = ss;
  return result;
}

The following is in another .cpp file.

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double F2(NumericVector a) {
  double result=0;

  result = F1(a);

  return result;
}
2
This is vague. Please show the code you are trying to use.Romain Francois
Thanks. I understand what you are trying to do now.Romain Francois
Thanks for sharing the code. What you do here would not work in plain C++ either has the second C++ file has no declaration of F1. Typically, one would have common declarations in a header. What Romain showed in his answer works, but must be considered a hack. Learn about multi-file C++ projects. Or keep things in one file.Dirk Eddelbuettel
You can actually create the header file for the first script, say first.h and write #include "a.h" in the second cpp file to import the functions in the first. Usually, "a.h" declares the signature of the functions and "a.cpp" implements it.Kota Mori

2 Answers

13
votes

Just put both functions in the same .cpp file, or start working on a package.

If you stick to separate .cpp files, then F2 does not know about F1. You can call back F1 as an R function, but that's not going to be as efficient and you will have to deal with converting outputs to a double, etc ...

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double F2(NumericVector a) {
  double result=0;

  // grab the R function F1
  Function F1( "F1" ) ; 
  result = as<double>( F1(a) );

  return result;
}

But really create a package or put all your functions in the same .cpp file.

4
votes

Couple of points:

  • Rcpp Attributes is not the only way to export C++ functions to R.

  • Rcpp Attributes renames functions, use the verbose=TRUE argument and see the result. These functions names are randomized but ...

  • Rcpp Attributes has an export-to-C++ ability, see Rcpp::interfaces() in the vignette.

So if you want to stick with Attributes that seems to be one way. Else call the function you want to call fworker(), have f1() call it (and f1() becomes available in R) and have f2() call fworker(). But you should be able to do better.

Otherwise, you can of course fall back to manually exporting the function using an explicitly created R wrapper.