<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<META name=GENERATOR content="MSHTML 8.00.7600.16588">
<STYLE>@font-face {
        font-family: 宋体;
}
@font-face {
        font-family: Verdana;
}
@font-face {
        font-family: @宋体;
}
@page Section1 {size: 595.3pt 841.9pt; margin: 72.0pt 90.0pt 72.0pt 90.0pt; layout-grid: 15.6pt; }
P.MsoNormal {
        TEXT-JUSTIFY: inter-ideograph; TEXT-ALIGN: justify; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman"; FONT-SIZE: 10.5pt
}
LI.MsoNormal {
        TEXT-JUSTIFY: inter-ideograph; TEXT-ALIGN: justify; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman"; FONT-SIZE: 10.5pt
}
DIV.MsoNormal {
        TEXT-JUSTIFY: inter-ideograph; TEXT-ALIGN: justify; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman"; FONT-SIZE: 10.5pt
}
A:link {
        COLOR: blue; TEXT-DECORATION: underline
}
SPAN.MsoHyperlink {
        COLOR: blue; TEXT-DECORATION: underline
}
A:visited {
        COLOR: purple; TEXT-DECORATION: underline
}
SPAN.MsoHyperlinkFollowed {
        COLOR: purple; TEXT-DECORATION: underline
}
SPAN.EmailStyle17 {
        FONT-STYLE: normal; FONT-FAMILY: Verdana; COLOR: windowtext; FONT-WEIGHT: normal; TEXT-DECORATION: none; mso-style-type: personal-compose
}
DIV.Section1 {
        page: Section1
}
BLOCKQUOTE {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; MARGIN-LEFT: 2em
}
OL {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px
}
UL {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px
}
</STYLE>
</HEAD>
<BODY style="FONT-FAMILY: verdana; FONT-SIZE: 10pt">
<DIV><FONT color=#0000ff size=2 face=Verdana>Thanks for your advice! I'll plan
to read the documents carefully. Now I am anxious to make the program
running. If possible, could you please give me an example to achieve that?
</FONT></DIV>
<DIV><FONT color=#0000ff></FONT> </DIV>
<DIV><FONT color=#0000ff>Best regards,</FONT></DIV>
<DIV><FONT color=#0000ff>Gao Xia</FONT></DIV>
<DIV><FONT size=2 face=Verdana></FONT> </DIV>
<DIV align=left>
<DIV align=left><FONT size=2 face=Verdana>
<HR style="WIDTH: 122px; HEIGHT: 2px" SIZE=2>
</FONT></DIV>
<DIV><FONT color=#c0c0c0><FONT size=2
face=Verdana><SPAN>xiagao1982</SPAN></FONT></DIV>
<DIV><FONT size=2 face=Verdana>2010-06-16</FONT></FONT></DIV></DIV>
<DIV><FONT size=2 face=Verdana>
<HR>
</FONT></DIV>
<DIV><FONT face=Verdana><FONT size=2><STRONG>发件人:</STRONG> Dirk
Eddelbuettel</FONT></FONT></DIV>
<DIV><FONT face=Verdana><FONT size=2><STRONG>发送时间:</STRONG>
2010-06-16 09:14:07</FONT></FONT></DIV>
<DIV><FONT face=Verdana><FONT size=2><STRONG>收件人:</STRONG>
xiagao1982</FONT></FONT></DIV>
<DIV><FONT face=Verdana><FONT size=2><STRONG>抄送:</STRONG>
rcpp-devel</FONT></FONT></DIV>
<DIV><FONT face=Verdana><FONT size=2><STRONG>主题:</STRONG> Re: [Rcpp-devel] About
calling C/C++ functions in R</FONT></FONT></DIV>
<DIV><FONT size=2 face=Verdana></FONT> </DIV>
<DIV><FONT size=2 face=Verdana>
<DIV></DIV>
<DIV>On 16 June 2010 at 08:13, xiagao1982 wrote:</DIV>
<DIV>| Dear friends,</DIV>
<DIV>| </DIV>
<DIV>| I am a newcomer of Rcpp and RInside. I installed them in my system and</DIV>
<DIV>| successfully build the following example:</DIV>
<DIV>| </DIV>
<DIV>| #include <RInside.h> //</DIV>
<DIV>| for the embedded R via RInside</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>| 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>| RInside R(argc, argv); //</DIV>
<DIV>| create an embedded R instance </DIV>
<DIV>| </DIV>
<DIV>| Rcpp::NumericMatrix M = createMatrix(mdim); //</DIV>
<DIV>| create and fill a sample data Matrix </DIV>
<DIV>| R["M"] = M; //</DIV>
<DIV>| assign C++ matrix M to R's 'M' var</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>| ans = R.parseEval(evalstr); //</DIV>
<DIV>| eval the init string -- Z is now in ans</DIV>
<DIV>| </DIV>
<DIV>| Rcpp::NumericVector v(ans); //</DIV>
<DIV>| 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>| const char* hello( std::string who ){</DIV>
<DIV></DIV>
<DIV>This defines a C++ function called "hello".</DIV>
<DIV></DIV>
<DIV>| std::string result( "hello " ) ;</DIV>
<DIV>| 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>| std::string txt = "ret = hello('Friends'); print(ret);";</DIV>
<DIV></DIV>
<DIV>This calls an R function called "hello".</DIV>
<DIV></DIV>
<DIV>But as R != C++, it does not call the C++ function from above.</DIV>
<DIV></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>It is a bit more complicated than this. You probably want to start by reading</DIV>
<DIV>both the 'Writing R Extensions' manual and some of the introductory vignettes</DIV>
<DIV>for Rcpp. It can be done, but you need to do a little reading, experimenting</DIV>
<DIV>and learning first.</DIV>
<DIV></DIV>
<DIV>-- </DIV>
<DIV> Regards, Dirk</DIV></FONT></DIV></BODY></HTML>