0

Is there a way to use an Rcpp function inside an R6 class while developing a package? Example: I have a add.cpp in the /src folder of my package as follows

#include <Rcpp.h>
using namespace Rcpp;

//
//' Add two numbers 
//'
//' @param x An integer.
//' @param y An integer
// [[Rcpp::export]]
int add(int x, int y) {
  return x + y;
}

I wish to use the Rcpp add function as a public function inside my R6 class called Numbers, which resides in the Numbers.R file in the /R folder of my package

Numbers <- R6class(
"Number",
private = list(
a =6,
b=10
),
public = list(
# How to use add function from add.cpp file using private$a and private$b as inputs 
) 
)
Gompu
  • 415
  • 1
  • 6
  • 21

1 Answers1

1

You can simply call the function. Am I missing something?

Numbers <- R6::R6Class(
  "Number",
  private = list(
    a = 6,
    b = 10
  ),
  public = list(
    add_ab = function() {
      add(private$a, private$b)
    }
  ) 
)
# Example
num <- Numbers$new()
num$add_ab()
F. Privé
  • 11,423
  • 2
  • 27
  • 78
  • I was curious if this add.cpp function will be exposed to an R6 class inside a package structure, as in, do we have to do something extra to the Namespace and Description file or anything at all. – Gompu Mar 06 '18 at 13:46
  • You may need to use `@import`, but I think it should be OK. – F. Privé Mar 06 '18 at 14:15