Skip to content

Instantly share code, notes, and snippets.

@doobwa
Created January 9, 2012 19:35
Show Gist options
  • Save doobwa/1584535 to your computer and use it in GitHub Desktop.
Save doobwa/1584535 to your computer and use it in GitHub Desktop.
Rcpp Module example
# Borrowed from http://dirk.eddelbuettel.com/papers/rcpp_workshop_part_3_advanced.pdf
require(Rcpp)
require(inline)
require(testthat)
fx <- cxxfunction(,"",includes=
'
double norm( double x, double y ){
return sqrt( x*x + y*y) ;
}
RCPP_MODULE(foo){
function( "norm", &norm ) ;
}
', plugin="Rcpp")
foo <- Module("foo",getDynLib(fx))
norm <- function(x,y) sqrt(x*x + y*y)
test_that("Rcpp module agrees with R version",{
expect_that(foo$norm(3,5),equals(norm(3,5)))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment