Question

Preliminary steps

QuantLib was installed along with Boost and built following these instructions in Microsoft Visual C++ 2010; test code went on with no issues.

Using R with the following sample code gave the expected results:

install.packages("Rcpp")
library(Rcpp)

cppFunction('
  int add(int x, int y, int z) {
    int sum = x + y + z;
    return sum;
  }'
)

add(1, 2, 3)
# > add(1, 2, 3)
# [1] 6

As of the use of separate C++ files, the example below

#include <Rcpp.h>
using namespace Rcpp;

// Below is a simple example of exporting a C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp 
// function (or via the Source button on the editor toolbar)

// For more on using Rcpp click the Help button on the editor toolbar

// [[Rcpp::export]]
int timesTwo(int x) {
   return x * 2;
}

succeded for the result in R was

> timesTwo(7)
[1] 14

I guess that everything is fine.

My question

If my setup is correct, my question is: assuming my QuantLib-vc100-mt-gd.lib object file library is in C:\DevTools\QuantLib-1.3\lib, what should I do to make something like the code below to work properly if called from R?

#include <ql/quantlib.hpp>
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double timesTwo(double x) {
  QuantLib::Calendar myCal = QuantLib::UnitedKingdom();
  QuantLib::Date newYearsEve(31, QuantLib::Dec, 2008);
  QuantLib::Rate zc3mQuote = x;
  return zc3mQuote * 2;
}
Was it helpful?

Solution

Please see the Rcpp FAQ for the general question of 'can I use R and Rcpp with Visual Studio' (tl;dr: No, you can't).

But before there was Rcpp there was already RQuantLib, and it still exists. Download its sources, download the quantlib-1.4.zip from the 'extras' site in Oxford and just rebuild RQuantLib with it. Which uses Rcpp.

You can then extend RQuantLib to your heart's content.

The newest RQuantLib also has a plugin similar to what RcppArmadillo and RcppEigen have, so you can build the quick little test files like the one you posted. I will try to follow-up on the weekend with an existence proof example.

Edit: As promised, I gave that a go. With the current RQuantLib (0.3.12) and Rcpp (0.11.1, released today but 0.11.0 should work) and your file save in /tmp/lisaann.cpp this "just works":

R> library(Rcpp)
R> sourceCpp("/tmp/lisaann.cpp")
R> timesTwo(1.23)
[1] 2.46
R> 

If it fails for you on Windows, make sure have

  • Rtools installed
  • the pre-build QuantLib for use by R (see my recent blog post)
  • have the environment variables set which src/Makevars.win expects

Else, just use Ubuntu, Debian, or any other sane OS in a virtual machine.

Edit 2: One important part, though, is that the [[ Rcpp::depends() ]] attribute is added to your code. With that, here is the file I used:

#include <ql/quantlib.hpp>
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::depends(RQuantLib)]]

// [[Rcpp::export]]
double timesTwo(double x) {
  QuantLib::Calendar myCal = QuantLib::UnitedKingdom();
  QuantLib::Date newYearsEve(31, QuantLib::Dec, 2008);
  QuantLib::Rate zc3mQuote = x;
  return zc3mQuote * 2;
}

which differs from yours only in the (important!) reference to the plugin used here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top