[Rcpp-devel] Using Rcpp in C++

Silkworth,David J. SILKWODJ at airproducts.com
Sat Jun 11 14:59:28 CEST 2011


You obviously want to get stated.  I have to assume that you use R,
because you would not have found Rcpp any other way.
Rcpp is a means of writing functions in C++ that R can call.  So you
don't expect to compile a stand-alone program with main().
The easiest way to experience Rcpp is using inline code, we need to have
the package "inline" installed so we can reference its library.

Here is an R script that will do a little more than you wished.

library(Rcpp)
library(inline)

src <- '
RNGScope scope;
Rcpp::IntegerVector num_rands(arg1);
Rcpp::NumericVector output(num_rands[0]);
output=runif( num_rands[0],0, 1 ) ;
return output;
'
 test_function <- cxxfunction(signature(arg1 = "integer"), src, plugin =
"Rcpp")


This script will create a function in your R session called
test_function.  You will not see it in the object list, but it is there.
It takes a single integer argument.
So after running the script above in the R Console you can then call the
function like this:

test_function(5)

in the R Console.

Soeren Vogel wrote:

Hello I want to experiment a little with Rcpp, however, as a newby I
don't get it to work, e.g.:

#include <iostream>
#include <Rcpp.h>
int main()
{
    using namespace Rcpp;
    RNGScope scope;
    std::cout << runif( 1, 0, 1) << std::endl;
    return 0;
}

How can I get it to work? Thanks, S?ren



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110611/2f7dfad9/attachment.htm>


More information about the Rcpp-devel mailing list