RInside Version 0.2.12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rinside_sample2.cpp
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8; -*-
2 //
3 // Simple example for the repeated r-devel mails by Abhijit Bera
4 //
5 // Copyright (C) 2009 Dirk Eddelbuettel
6 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
7 
8 #include <RInside.h> // for the embedded R via RInside
9 
10 int main(int argc, char *argv[]) {
11 
12  try {
13  RInside R(argc, argv); // create an embedded R instance
14 
15  std::string txt = "suppressMessages(library(fPortfolio))";
16  R.parseEvalQ(txt); // load library, no return value
17 
18  txt = "M <- as.matrix(SWX.RET); print(head(M)); M";
19  Rcpp::NumericMatrix M = R.parseEval(txt); // assign mat. M to NumericMatrix
20 
21  std::cout << "M has "
22  << M.nrow() << " rows and "
23  << M.ncol() << " cols" << std::endl;
24 
25  txt = "colnames(M)"; // assign columns names of M to ans and
26  Rcpp::CharacterVector cnames = R.parseEval(txt); // into str.vec. cnames
27 
28  for (int i=0; i<M.ncol(); i++) {
29  std::cout << "Column " << cnames[i] << " in row 42 has " << M(42,i) << std::endl;
30  }
31 
32  } catch(std::exception& ex) {
33  std::cerr << "Exception caught: " << ex.what() << std::endl;
34  } catch(...) {
35  std::cerr << "Unknown exception caught" << std::endl;
36  }
37 
38  exit(0);
39 }
40 
void parseEvalQ(const std::string &line)
Definition: RInside.cpp:366
int parseEval(const std::string &line, SEXP &ans)
Definition: RInside.cpp:308
int main(int argc, char *argv[])