From edd at debian.org  Fri Aug 28 17:36:31 2009
From: edd at debian.org (Dirk Eddelbuettel)
Date: Fri, 28 Aug 2009 10:36:31 -0500
Subject: [Rd] Simpler example of embedding R in C++ (Was: Clarifications
	please.)
In-Reply-To: <BFC7DAF8-F59E-4901-B79E-3F3BF593DC73@r-project.org>
References: <fc212e850908250541i16b1918h58187248193b4cbf@mail.gmail.com>
	<6phd46km2gl.fsf@gopher4.fhcrc.org>
	<fc212e850908260007w616bc806wccfe5e89d33b3466@mail.gmail.com>
	<fc212e850908260715u749f0c3dtfd8599477cb7c805@mail.gmail.com>
	<4A95690E.1070806@fhcrc.org>
	<fc212e850908280441r7c316b68se9192913e30cdc0d@mail.gmail.com>
	<E2BDA7E3-D753-4CA9-AB3B-A36755AFCEA0@r-project.org>
	<fc212e850908280642j6c75f749p5961130cc5e1d509@mail.gmail.com>
	<BFC7DAF8-F59E-4901-B79E-3F3BF593DC73@r-project.org>
Message-ID: <19095.63871.980598.801338@ron.nulle.part>


Abhijit,

I think you are making your life harder than you need to.  Consider this
simple C++ program implementing your example (of loading fPortfolio,
converting the data in SWX.RET to a matrix M, exporting M to C++ and showing
its content):


  edd at ron:~/svn/rinside/pkg/inst/examples> cat rinside_test2.cpp
  // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; -*-
  //
  // Simple example for the repeated r-devel mails 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
  
  int main(int argc, char *argv[]) {
  
      RInside R(argc, argv);		// create an embedded R instance 
      SEXP ans;
  
      std::string txt = "suppressMessages(library(fPortfolio))";
      R.parseEvalQ(txt); 		// load library, no return valueassign matrix M to SEXP variable ans
  
      txt = "M <- as.matrix(SWX.RET); print(head(M)); M";
      R.parseEval(txt, ans); 		// assign matrix M to SEXP variable ans
      RcppMatrix<double> M(ans);	// convert SEXP variable to an RcppMatrix
  
      std::cout << "M has " << M.getDim1() << " rows and " << M.getDim2() << " cols" << std::endl;
  
      R.parseEval("colnames(M)", ans); 	// assign columns names of M to ans
      RcppStringVector cnames(ans);	// and into string vector cnames
  
      for (int i=0; i<M.getDim2(); i++) {
  	std::cout << "Column " << cnames(i) << " in row 42 has " << M(42,i) << std::endl;
      }
  
      exit(0);
  }


Compiling and linking against the RInside (for the embedded R) and Rcpp (for
the type conversion) libraries (using a simple 'make rinside_test2' using an
implicit make rule in the examples/ directory), I get this program:


  edd at ron:~/svn/rinside/pkg/inst/examples> ./rinside_test2
  Package 'sn', 0.4-12 (2009-03-21). Type 'help(SN)' for summary information
  Using the GLPK callable library version 4.37
                    SBI       SPI        SII      LP25      LP40      LP60
  2000-01-04 -0.0020881 -0.034390  1.367e-05 -0.011994 -0.018013 -0.026155
  2000-01-05 -0.0001045 -0.010408 -4.955e-03 -0.003657 -0.005837 -0.009011
  2000-01-06 -0.0013598  0.012119  3.813e-03 -0.001324 -0.001645 -0.002396
  2000-01-07  0.0004186  0.022462 -6.162e-04  0.007715  0.011660  0.017063
  2000-01-10  0.0000000  0.002108  2.381e-03  0.003029  0.004566  0.006948
  2000-01-11 -0.0010468 -0.002774 -2.938e-04 -0.002423 -0.003143 -0.004183
  M has 1916 rows and 6 cols
  Column SBI in row 42 has -0.000848356
  Column SPI in row 42 has 0.0129016
  Column SII in row 42 has -0.000141168
  Column LP25 in row 42 has 0.00261833
  Column LP40 in row 42 has 0.00443192
  Column LP60 in row 42 has 0.00694762
  edd at ron:~/svn/rinside/pkg/inst/examples> 


This took literally a few minutes to put together, given the infrastructure
provided by Rcpp and RInside.

Comments welcome.  I know I still need to enhance the installation
instructions for RInside -- it works out of the box for Debian / Ubuntu on 32
and 64 bit, but another user on FC 64bit alerted me to issues he experienced
which I need to fix.

Hth,  Dirk

-- 
Three out of two people have difficulties with fractions.



[Rd] Simpler example of embedding R in C++ (Was: Clarifications please.)
Dirk Eddelbuettel