Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
However, today I committed a new example to SVN archive at R-Forge. It is based on this thread on r-devel. Abhijit Bera tries to do this in C, but to me his questions provide rather clear motivation for showing how much simpler things can be via C++ and the Rcpp classes along with RInside. Using a small example, the task was to pass a weight vector to a portfolio solver from the Rmetrics package fPortfolio and to then access the computed solution. The original poster struggled with access from C to the S4 classes used by fPortfolio and could not set the weights. But when using RInside, we simply pass a C++ vector of weights down to R, solve the problem and pass a solution vector back using the handy evaluation of R expressions: < !-- done highlight -i rinside_sample4.cpp -o rinside_sample4.cpp.html --html --style=emacs --inline-css -->
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8; -*- // // Another simple example inspired by an r-devel mail by Abhijit Bera // // Copyright (C) 2009 Dirk Eddelbuettel and GPL'ed #include "RInside.h" // for the embedded R via RInside #include "Rcpp.h" // for the R / Cpp interface used for transfer #include <iomanip> int main(int argc, char *argv[]) { try { RInside R(argc, argv); // create an embedded R instance SEXP ans; std::string txt = "suppressMessages(library(fPortfolio))"; if (R.parseEvalQ(txt)) // load library, no return value throw std::runtime_error("R cannot evaluate '" + txt + "'"); txt = "lppData <- 100 * LPP2005.RET[, 1:6]; " "ewSpec <- portfolioSpec(); " "nAssets <- ncol(lppData); "; if (R.parseEval(txt, ans)) // prepare problem throw std::runtime_error("R cannot evaluate '" + txt + "'"); const double dvec[6] = { 0.1, 0.1, 0.1, 0.1, 0.3, 0.3 }; // choose any weights you want const std::vector<double> w(dvec, &dvec[6]); R.assign( w, "weightsvec"); // assign STL vector to R's 'weightsvec' variable txt = "setWeights(ewSpec) <- weightsvec"; if (R.parseEvalQ(txt)) // evaluate assignment throw std::runtime_error("R cannot evaluate '" + txt + "'"); txt = "ewPortfolio <- feasiblePortfolio(data = lppData, spec = ewSpec, constraints = \"LongOnly\"); " "print(ewPortfolio); " "vec <- getCovRiskBudgets(ewPortfolio@portfolio)"; if (R.parseEval(txt, ans)) // assign covRiskBudget weights to ans throw std::runtime_error("R cannot evaluate '" + txt + "'"); RcppVector<double> V(ans); // convert SEXP variable to an RcppMatrix R.parseEval("names(vec)", ans); // assign columns names to ans RcppStringVector names(ans); for (int i=0; i<names.size(); i++) { std::cout << std::setw(16) << names(i) << "\t" << std::setw(11) << V(i) << "\n"; } } catch(std::exception& ex) { std::cerr << "Exception caught: " << ex.what() << std::endl; } catch(...) { std::cerr << "Unknown exception caught" << std::endl; } exit(0); }
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.