[Rcpp-devel] About calling C/C++ functions in R

Romain Francois romain at r-enthusiasts.com
Wed Jun 16 11:15:27 CEST 2010


Le 16/06/10 10:56, Romain Francois a écrit :
>
> Hi,
>
> I totally second Dirk in the advice about reading the appropriate
> documents. I would add the book from John Chambers "Programming with
> Data" as it has very clear chapters about R and C(++).
>
> Once you digested these documents, you can read the Rcpp-modules
> vignette from Rcpp, which might help you.
>
> Also, I just commited some code in Rcpp (which will be available in the
> next version of Rcpp : 0.8.3) that adds the Rcpp::InternalFunction
> class, that you can use like this:
>
> // grab the global environment
> Environment g = Environment::global_env() ;
>
> // assign the "hello" R variable to the internal function
> g["hello"] = InternalFunction( &hello ) ;
>
>
>
> Here is a complete example you can run from the R prompt:
>
> require( Rcpp)
> require( inline)
>
> inc <- '
>
> const char* hello( std::string who ){
> std::string result( "hello " ) ;
> result += who ;
> return result.c_str() ;
> }
>
> '
> code <- '
> Environment g = Environment::global_env() ;
> g["hello"] = InternalFunction( &hello ) ;
> return R_NilValue ;
> '
> fx <- cxxfunction( signature(), code, inc, plugin = "Rcpp" )
> f <- fx() # force loading the dynamic library
>
> hello( "world" )
>
> I have not tried it yet from RInside.

Done. I added this as a new example "rinside_sample9.cpp".



// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
//
// Simple example showing how expose a C++ function
//
// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois

#include <RInside.h>                    // for the embedded R via RInside

// a c++ function we wish to expose to R
const char* hello( std::string who ){
         std::string result( "hello " ) ;
         result += who ;
         return result.c_str() ;
}

int main(int argc, char *argv[]) {

	// create an embedded R instance
     RInside R(argc, argv);

     // expose the "hello" function in the global environment
     R["hello"] = Rcpp::InternalFunction( &hello ) ;

     // call it and display the result
     std::string result = R.parseEval("hello('world')") ;
     std::cout << "hello( 'world') =  " << result << std::endl ;

     exit(0);
}


Romain


> Romain
>
> Le 16/06/10 02:13, xiagao1982 a écrit :
>> Dear friends,
>> I am a newcomer of Rcpp and RInside. I installed them in my system and
>> successfully build the following example:
>> #include <RInside.h> // for the embedded R via RInside
>> Rcpp::NumericMatrix createMatrix(const int n) {
>> 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;
>> }
>> }
>> 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
>> 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
>> 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
>> for (int i=0; i< v.size(); i++) { // show the result
>> std::cout << "In C++ element " << i << " is " << v[i] << std::endl;
>> }
>> exit(0);
>> }
>> Now I add a function in the C++ code:
>> const char* hello( std::string who ){
>> std::string result( "hello " ) ;
>> result += who ;
>> return result.c_str() ;
>> }
>> And I try to call this function in R:
>> std::string txt = "ret = hello('Friends'); print(ret);";
>> R.parseEvalQ(txt); // eval string quietly, no result
>> It fails to do that.
>> Could you please tell me how to achieve that? Thanks very much!
>> Gao Xia
>> ------------------------------------------------------------------------
>> xiagao1982
>> 2010-06-16
>
>


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/98Uf7u : Rcpp 0.8.1
|- http://bit.ly/c6YnCi : graph gallery collage
`- http://bit.ly/bZ7ltC : inline 0.3.5



More information about the Rcpp-devel mailing list