[Rcpp-devel] How to access fields and methods of a RefClass from C++?

Anwar Ludin anwar.ludin at riskcetera.com
Tue May 28 23:14:09 CEST 2013


Hello,

I have a Reference class defined in R that I am passing as a parameter 
to a C++ class using Rcpp. I am trying to access the fields of the 
Reference class from C++ and I'm not sure how to do this.

instrument.R:

Instrument <-setRefClass(
   Class="Instrument",
   fields=list("id"="character", "description"="character")
)

Instrument$accessors(c("id", "description"))

PricingEngine.cpp:

#include <Rcpp.h>

class PricingEngine;

RCPP_EXPOSED_CLASS(PricingEngine)

using namespace Rcpp;

class PricingEngine{
   public:
   PricingEngine(){};
   virtual ~PricingEngine(){};

   double price(SEXP value){
     Rcpp::S4 obj(value);
     // How do I check that SEXP is an instance of Instrument
     // How do use accessors  and fields of Instrument?
     // do pricing here
     return price;
   }

};

RCPP_MODULE(riskceteraPricing) {
    class_<PricingEngine>("PricingEngine")
   .constructor()
   .method("price", &PricingEngine::price)
   ;
}

// R REPL

instrument <- Instrument$new(id="AAPL", description="Apple")
pricingEngine <- new(PricingEngine)
price <- pricingEngine$price(instrument)






More information about the Rcpp-devel mailing list