RInside Version 0.2.12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rinside_sample3.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 using lm() using the example from help(swiss)
4 //
5 // Copyright (C) 2009 Dirk Eddelbuettel
6 // Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
7 
8 #include <RInside.h> // for the embedded R via RInside
9 #include <iomanip>
10 
11 int main(int argc, char *argv[]) {
12 
13  RInside R(argc, argv); // create an embedded R instance
14 
15  std::string txt = // load library, run regression, create summary
16  "suppressMessages(require(stats));"
17  "swisssum <- summary(lm(Fertility ~ . , data = swiss));"
18  "print(swisssum)";
19  R.parseEvalQ(txt); // eval command, no return
20 
21  // evaluate R expressions, and assign directly into Rcpp types
22  Rcpp::NumericMatrix M( (SEXP) R.parseEval("swcoef <- coef(swisssum)"));
23  Rcpp::StringVector cnames( (SEXP) R.parseEval("colnames(swcoef)"));
24  Rcpp::StringVector rnames( (SEXP) R.parseEval("rownames(swcoef)"));
25 
26  std::cout << "\n\nAnd now from C++\n\n\t\t\t";
27  for (int i=0; i<cnames.size(); i++) {
28  std::cout << std::setw(11) << cnames[i] << "\t";
29  }
30  std::cout << std::endl;
31  for (int i=0; i<rnames.size(); i++) {
32  std::cout << std::setw(16) << rnames[i] << "\t";
33  for (int j=0; j<cnames.size(); j++) {
34  std::cout << std::setw(11) << M(i,j) << "\t";
35  }
36  std::cout << std::endl;
37  }
38  std::cout << std::endl;
39 
40  exit(0);
41 }
42 
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[])