<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=utf-8" 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 help! When will the
new version 0.8.3 be released?</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> Romain
Francois</FONT></FONT></DIV>
<DIV><FONT face=Verdana><FONT size=2><STRONG>发送时间:</STRONG>
2010-06-16 17:15:35</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>Le 16/06/10 10:56, Romain Francois a écrit :</DIV>
<DIV>></DIV>
<DIV>> Hi,</DIV>
<DIV>></DIV>
<DIV>> I totally second Dirk in the advice about reading the appropriate</DIV>
<DIV>> documents. I would add the book from John Chambers "Programming with</DIV>
<DIV>> Data" as it has very clear chapters about R and C(++).</DIV>
<DIV>></DIV>
<DIV>> Once you digested these documents, you can read the Rcpp-modules</DIV>
<DIV>> vignette from Rcpp, which might help you.</DIV>
<DIV>></DIV>
<DIV>> Also, I just commited some code in Rcpp (which will be available in the</DIV>
<DIV>> next version of Rcpp : 0.8.3) that adds the Rcpp::InternalFunction</DIV>
<DIV>> class, that you can use like this:</DIV>
<DIV>></DIV>
<DIV>> // grab the global environment</DIV>
<DIV>> Environment g = Environment::global_env() ;</DIV>
<DIV>></DIV>
<DIV>> // assign the "hello" R variable to the internal function</DIV>
<DIV>> g["hello"] = InternalFunction( &hello ) ;</DIV>
<DIV>></DIV>
<DIV>></DIV>
<DIV>></DIV>
<DIV>> Here is a complete example you can run from the R prompt:</DIV>
<DIV>></DIV>
<DIV>> require( Rcpp)</DIV>
<DIV>> require( inline)</DIV>
<DIV>></DIV>
<DIV>> inc <- '</DIV>
<DIV>></DIV>
<DIV>> const char* hello( std::string who ){</DIV>
<DIV>> std::string result( "hello " ) ;</DIV>
<DIV>> result += who ;</DIV>
<DIV>> return result.c_str() ;</DIV>
<DIV>> }</DIV>
<DIV>></DIV>
<DIV>> '</DIV>
<DIV>> code <- '</DIV>
<DIV>> Environment g = Environment::global_env() ;</DIV>
<DIV>> g["hello"] = InternalFunction( &hello ) ;</DIV>
<DIV>> return R_NilValue ;</DIV>
<DIV>> '</DIV>
<DIV>> fx <- cxxfunction( signature(), code, inc, plugin = "Rcpp" )</DIV>
<DIV>> f <- fx() # force loading the dynamic library</DIV>
<DIV>></DIV>
<DIV>> hello( "world" )</DIV>
<DIV>></DIV>
<DIV>> I have not tried it yet from RInside.</DIV>
<DIV></DIV>
<DIV>Done. I added this as a new example "rinside_sample9.cpp".</DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV>// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8; -*-</DIV>
<DIV>//</DIV>
<DIV>// Simple example showing how expose a C++ function</DIV>
<DIV>//</DIV>
<DIV>// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois</DIV>
<DIV></DIV>
<DIV>#include <RInside.h> // for the embedded R via RInside</DIV>
<DIV></DIV>
<DIV>// a c++ function we wish to expose to R</DIV>
<DIV>const char* hello( std::string who ){</DIV>
<DIV> std::string result( "hello " ) ;</DIV>
<DIV> result += who ;</DIV>
<DIV> return result.c_str() ;</DIV>
<DIV>}</DIV>
<DIV></DIV>
<DIV>int main(int argc, char *argv[]) {</DIV>
<DIV></DIV>
<DIV>// create an embedded R instance</DIV>
<DIV> RInside R(argc, argv);</DIV>
<DIV></DIV>
<DIV> // expose the "hello" function in the global environment</DIV>
<DIV> R["hello"] = Rcpp::InternalFunction( &hello ) ;</DIV>
<DIV></DIV>
<DIV> // call it and display the result</DIV>
<DIV> std::string result = R.parseEval("hello('world')") ;</DIV>
<DIV> std::cout << "hello( 'world') = " << result << std::endl ;</DIV>
<DIV></DIV>
<DIV> exit(0);</DIV>
<DIV>}</DIV>
<DIV></DIV>
<DIV></DIV>
<DIV>Romain</DIV>
<DIV></DIV>
<DIV></DIV>
<DIV>> Romain</DIV>
<DIV>></DIV>
<DIV>> Le 16/06/10 02:13, xiagao1982 a écrit :</DIV>
<DIV>>> Dear friends,</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>>> #include <RInside.h> // for the embedded R via RInside</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); // create an embedded R instance</DIV>
<DIV>>> Rcpp::NumericMatrix M = createMatrix(mdim); // create and fill a sample</DIV>
<DIV>>> data Matrix</DIV>
<DIV>>> R["M"] = M; // 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); // eval the init string -- Z is now in ans</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>>> Now I add a function in the C++ code:</DIV>
<DIV>>> const char* hello( std::string who ){</DIV>
<DIV>>> std::string result( "hello " ) ;</DIV>
<DIV>>> result += who ;</DIV>
<DIV>>> return result.c_str() ;</DIV>
<DIV>>> }</DIV>
<DIV>>> And I try to call this function in R:</DIV>
<DIV>>> std::string txt = "ret = hello('Friends'); print(ret);";</DIV>
<DIV>>> R.parseEvalQ(txt); // eval string quietly, no result</DIV>
<DIV>>> It fails to do that.</DIV>
<DIV>>> Could you please tell me how to achieve that? Thanks very much!</DIV>
<DIV>>> Gao Xia</DIV>
<DIV>>> ------------------------------------------------------------------------</DIV>
<DIV>>> xiagao1982</DIV>
<DIV>>> 2010-06-16</DIV>
<DIV>></DIV>
<DIV>></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV>-- </DIV>
<DIV>Romain Francois</DIV>
<DIV>Professional R Enthusiast</DIV>
<DIV>+33(0) 6 28 91 30 30</DIV>
<DIV>http://romainfrancois.blog.free.fr</DIV>
<DIV>|- http://bit.ly/98Uf7u : Rcpp 0.8.1</DIV>
<DIV>|- http://bit.ly/c6YnCi : graph gallery collage</DIV>
<DIV>`- http://bit.ly/bZ7ltC : inline 0.3.5</DIV>
<DIV></DIV></FONT></DIV></BODY></HTML>