[Rinside-commits] r207 - pkg/inst/examples/standard
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Apr 20 03:45:49 CEST 2011
Author: edd
Date: 2011-04-20 03:45:39 +0200 (Wed, 20 Apr 2011)
New Revision: 207
Modified:
pkg/inst/examples/standard/rinside_sample1.cpp
pkg/inst/examples/standard/rinside_sample2.cpp
Log:
minor cleanups
Modified: pkg/inst/examples/standard/rinside_sample1.cpp
===================================================================
--- pkg/inst/examples/standard/rinside_sample1.cpp 2011-04-19 17:15:04 UTC (rev 206)
+++ pkg/inst/examples/standard/rinside_sample1.cpp 2011-04-20 01:45:39 UTC (rev 207)
@@ -2,8 +2,8 @@
//
// Simple example with data in C++ that is passed to R, processed and a result is extracted
//
-// Copyright (C) 2009 Dirk Eddelbuettel
-// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2009 Dirk Eddelbuettel
+// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
//
// GPL'ed
@@ -13,30 +13,28 @@
Rcpp::NumericMatrix M(n,n);
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
- M(i,j) = i*10+j;
+ M(i,j) = i*10 + j;
}
}
return(M);
}
int main(int argc, char *argv[]) {
- const int mdim = 4; // let the matrices be 4 by 4
- SEXP ans;
RInside R(argc, argv); // create an embedded R instance
- Rcpp::NumericMatrix M = createMatrix(mdim); // create and fill a sample data Matrix
- R["M"] = M; // assign C++ matrix M to R's 'M' var
+ const int mdim = 4; // let the matrices be 4 by 4; create, fill
+ R["M"] = createMatrix(mdim); // then assign data Matrix to R's 'M' var
- std::string evalstr = "\
- cat('Running ls()\n'); print(ls()); \
- cat('Showing M\n'); print(M); \
- cat('Showing colSums()\n'); Z <- colSums(M); print(Z); \
- Z"; // returns Z
+ std::string str =
+ "cat('Running ls()\n'); print(ls()); "
+ "cat('Showing M\n'); print(M); "
+ "cat('Showing colSums()\n'); Z <- colSums(M); print(Z); "
+ "Z"; // returns Z
- ans = R.parseEval(evalstr); // eval the init string -- Z is now in ans
-
- Rcpp::NumericVector v(ans); // convert SEXP ans to a vector of doubles
+
+ Rcpp::NumericVector v = R.parseEval(str); // eval string, Z then assigned to num. vec
+
for (int i=0; i< v.size(); i++) { // show the result
std::cout << "In C++ element " << i << " is " << v[i] << std::endl;
}
Modified: pkg/inst/examples/standard/rinside_sample2.cpp
===================================================================
--- pkg/inst/examples/standard/rinside_sample2.cpp 2011-04-19 17:15:04 UTC (rev 206)
+++ pkg/inst/examples/standard/rinside_sample2.cpp 2011-04-20 01:45:39 UTC (rev 207)
@@ -2,8 +2,8 @@
//
// Simple example for the repeated r-devel mails by Abhijit Bera
//
-// Copyright (C) 2009 Dirk Eddelbuettel
-// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2009 Dirk Eddelbuettel
+// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
#include <RInside.h> // for the embedded R via RInside
@@ -11,25 +11,20 @@
try {
RInside R(argc, argv); // create an embedded R instance
- SEXP ans;
std::string txt = "suppressMessages(library(fPortfolio))";
R.parseEvalQ(txt); // load library, no return value
txt = "M <- as.matrix(SWX.RET); print(head(M)); M";
- ans = R.parseEval(txt); // assign matrix M to SEXP variable ans
+ Rcpp::NumericMatrix M = R.parseEval(txt); // assign mat. M to NumericMatrix
- Rcpp::NumericMatrix M(ans); // convert SEXP variable to a NumericMatrix
-
std::cout << "M has "
<< M.nrow() << " rows and "
<< M.ncol() << " cols" << std::endl;
- txt = "colnames(M)";
- ans = R.parseEval(txt); // assign columns names of M to ans
+ txt = "colnames(M)"; // assign columns names of M to ans and
+ Rcpp::CharacterVector cnames = R.parseEval(txt); // into str.vec. cnames
- Rcpp::CharacterVector cnames(ans); // and into string vector cnames
-
for (int i=0; i<M.ncol(); i++) {
std::cout << "Column " << cnames[i] << " in row 42 has " << M(42,i) << std::endl;
}
More information about the Rinside-commits
mailing list