3

I am writing an r package that utilizes rcpp.

I have the following class in C++:

using namespace std;

typedef float ffm_float;
typedef double ffm_double;
typedef int ffm_int;
typedef long long ffm_long;

class ffm_model {
  public:
    ffm_int n; // number of features
    ffm_int m; // number of fields
    ffm_int k; // number of latent factors
    ffm_float *w = nullptr;
    bool normalization;
    ~ffm_model();
};

And I wrote the following RCPP module in order to expose that class to r:

RCPP_MODULE(ffmModelMod){
  using namespace Rcpp;

  class_<ffm_model>( "ffm_model")

    .field("n", &ffm_model::n)
    .field("m", &ffm_model::m)
    .field("k", &ffm_model::k)
    .field("*w", &ffm_model::*w)
    .field("normalization", &ffm_model::normalization)
    .method("~ffm_model",&ffm_model::~ffm_model)
    ;
}

When I try to install the package in R, I run into issues since ffm_float *w cannot be cannot directly be converted to a SEXP. From a previous question I asked on here, it was recommended that I try utilizing XPtr to wrap the pointer. However, I'm having a lot of trouble finding helpful documentation, and I don't even know where to start.

In attempt to solve this issue, I have written the following forward declaration, but I have no idea if I'm even heading in the right direction.

namespace Rcpp {

    namespace traits{

        // Support for wrap
        template <typename T> SEXP wrap(const ffm_model<T>&);

        // Support for as<T>
        template <typename T> class Exporter< ffm_model<T> >;

    }
}

Can someone please point me in the right direction? I have already read the RCPP extending vignette.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ndroz
  • 31
  • 2
  • 2
    Welcome to StackOverflow and your second question. "How do I ...?" is not really appropriate here, and I suggest you subscribe to the rcpp-devel list and ask there. – Dirk Eddelbuettel Mar 28 '18 at 16:20
  • 1
    You are also spelling Rcpp the wrong way. It is not ALLUPPERCASE. – Dirk Eddelbuettel Mar 28 '18 at 16:20
  • What is the use of `w`? If it represents an array of floating point numbers and if the user is meant to read and/or write this property, then you could use a `NumericVector` together with appropriate getter and setter functions instead of a raw pointer in the interface. – Ralf Stubner Mar 29 '18 at 09:13

0 Answers0