RInside Version 0.2.6
inst/examples/standard/rinside_sample2.cpp
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
00002 //
00003 // Simple example for the repeated r-devel mails by Abhijit Bera
00004 //
00005 // Copyright (C) 2009         Dirk Eddelbuettel 
00006 // Copyright (C) 2010 - 2011  Dirk Eddelbuettel and Romain Francois
00007 
00008 #include <RInside.h>                    // for the embedded R via RInside
00009 
00010 int main(int argc, char *argv[]) {
00011 
00012     try {
00013         RInside R(argc, argv);          // create an embedded R instance 
00014 
00015         std::string txt = "suppressMessages(library(fPortfolio))";
00016         R.parseEvalQ(txt);              // load library, no return value
00017 
00018         txt = "M <- as.matrix(SWX.RET); print(head(M)); M";
00019         Rcpp::NumericMatrix M = R.parseEval(txt);  // assign mat. M to NumericMatrix
00020 
00021         std::cout << "M has " 
00022                   << M.nrow() << " rows and " 
00023                   << M.ncol() << " cols" << std::endl;
00024         
00025         txt = "colnames(M)";            // assign columns names of M to ans and
00026         Rcpp::CharacterVector cnames = R.parseEval(txt);   // into str.vec. cnames
00027 
00028         for (int i=0; i<M.ncol(); i++) {
00029             std::cout << "Column " << cnames[i] << " in row 42 has " << M(42,i) << std::endl;
00030         }
00031     
00032     } catch(std::exception& ex) {
00033         std::cerr << "Exception caught: " << ex.what() << std::endl;
00034     } catch(...) {
00035         std::cerr << "Unknown exception caught" << std::endl;
00036     }
00037 
00038     exit(0);
00039 }
00040 
 All Classes Files Functions Variables Enumerations Enumerator Defines