<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=us-ascii" http-equiv=Content-Type>
<META name=GENERATOR content="MSHTML 8.00.7600.16588"><LINK rel=stylesheet
href="BLOCKQUOTE{margin-Top: 0px; margin-Bottom: 0px; margin-Left: 2em}"></HEAD>
<BODY style="FONT-FAMILY: verdana; FONT-SIZE: 10pt">
<DIV><FONT face=Verdana><FONT size=2>Dear friends,</FONT></FONT></DIV>
<DIV><FONT face=Verdana></FONT> </DIV>
<DIV><FONT face=Verdana>I am a newcomer of Rcpp and RInside. I installed them in
my system and successfully build the following example:</FONT></DIV>
<DIV><FONT face=Verdana></FONT> </DIV>
<DIV><FONT face=Verdana>
<DIV>#include <RInside.h> // for the embedded R via RInside</DIV>
<DIV> </DIV>
<DIV></DIV>
<DIV>Rcpp::NumericMatrix createMatrix(const int n) {</DIV>
<DIV> Rcpp::NumericMatrix M(n,n);</DIV>
<DIV> for (int i=0; i<n; i++) {</DIV>
<DIV> for (int j=0; j<n; j++) {</DIV>
<DIV> M(i,j) = i*10+j; </DIV>
<DIV> }</DIV>
<DIV> }</DIV>
<DIV> return(M);</DIV>
<DIV>}</DIV>
<DIV> </DIV>
<DIV></DIV>
<DIV>int main(int argc, char *argv[]) {</DIV>
<DIV> const int mdim = 4; // let the matrices be 4 by 4 </DIV>
<DIV> SEXP ans;</DIV>
<DIV></DIV>
<DIV> RInside R(argc, argv); // create an embedded R instance </DIV>
<DIV> </DIV>
<DIV> Rcpp::NumericMatrix M = createMatrix(mdim); // create and fill a sample data Matrix </DIV>
<DIV> R["M"] = M; // assign C++ matrix M to R's 'M' var</DIV>
<DIV></DIV>
<DIV> std::string evalstr = "\</DIV>
<DIV> cat('Running ls()\n'); print(ls()); \</DIV>
<DIV> cat('Showing M\n'); print(M); \</DIV>
<DIV> cat('Showing colSums()\n'); Z <- colSums(M); print(Z); \</DIV>
<DIV> Z"; // returns Z</DIV>
<DIV></DIV>
<DIV> ans = R.parseEval(evalstr); // eval the init string -- Z is now in ans</DIV>
<DIV> </DIV>
<DIV> Rcpp::NumericVector v(ans); // convert SEXP ans to a vector of doubles</DIV>
<DIV> for (int i=0; i< v.size(); i++) { // show the result</DIV>
<DIV> std::cout << "In C++ element " << i << " is " << v[i] << std::endl;</DIV>
<DIV> }</DIV>
<DIV> exit(0);</DIV>
<DIV>}</DIV>
<DIV> </DIV>
<DIV>Now I add a function in the C++ code:</DIV>
<DIV> </DIV>
<DIV>
<DIV>const char* hello( std::string who ){</DIV>
<DIV> std::string result( "hello " ) ;</DIV>
<DIV style="TEXT-INDENT: 2em">result += who ;</DIV>
<DIV> return result.c_str() ;</DIV>
<DIV>}</DIV>
<DIV> </DIV>
<DIV>And I try to call this function in R:</DIV>
<DIV> </DIV>
<DIV>
<DIV>std::string txt = "ret = hello('Friends');
print(ret);";</DIV>
<DIV>R.parseEvalQ(txt); // eval string quietly, no result</DIV>
<DIV> </DIV>
<DIV>It fails to do that. </DIV>
<DIV> </DIV>
<DIV>Could you please tell me how to achieve that? Thanks very much!</DIV>
<DIV> </DIV>
<DIV>Gao Xia</DIV></DIV></DIV></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV align=left><FONT size=2>
<HR style="WIDTH: 122px; HEIGHT: 2px" SIZE=2>
</FONT></DIV>
<DIV><FONT color=#c0c0c0><FONT size=2><SPAN>xiagao1982</SPAN></FONT></DIV>
<DIV><FONT size=2>2010-06-16</FONT></FONT></DIV></FONT></BODY></HTML>