|
RInside Version 0.2.6
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8; -*- 00002 // 00003 // Simple example for using lm() using the example from help(swiss) 00004 // 00005 // Copyright (C) 2009 Dirk Eddelbuettel 00006 // Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois 00007 00008 #include <RInside.h> // for the embedded R via RInside 00009 #include <iomanip> 00010 00011 int main(int argc, char *argv[]) { 00012 00013 RInside R(argc, argv); // create an embedded R instance 00014 00015 std::string txt = // load library, run regression, create summary 00016 "suppressMessages(require(stats));" 00017 "swisssum <- summary(lm(Fertility ~ . , data = swiss));" 00018 "print(swisssum)"; 00019 R.parseEvalQ(txt); // eval command, no return 00020 00021 // evaluate R expressions, and assign directly into Rcpp types 00022 Rcpp::NumericMatrix M( (SEXP) R.parseEval("swcoef <- coef(swisssum)")); 00023 Rcpp::StringVector cnames( (SEXP) R.parseEval("colnames(swcoef)")); 00024 Rcpp::StringVector rnames( (SEXP) R.parseEval("rownames(swcoef)")); 00025 00026 std::cout << "\n\nAnd now from C++\n\n\t\t\t"; 00027 for (int i=0; i<cnames.size(); i++) { 00028 std::cout << std::setw(11) << cnames[i] << "\t"; 00029 } 00030 std::cout << std::endl; 00031 for (int i=0; i<rnames.size(); i++) { 00032 std::cout << std::setw(16) << rnames[i] << "\t"; 00033 for (int j=0; j<cnames.size(); j++) { 00034 std::cout << std::setw(11) << M(i,j) << "\t"; 00035 } 00036 std::cout << std::endl; 00037 } 00038 std::cout << std::endl; 00039 00040 exit(0); 00041 } 00042