Optymalizacja kodu R za pomocą Rcpp
Romain François
Consulting Datactive, ThinkR
evalCpp()
evalCpp( "40 + 2" )
42
cppFunction()
cppFunction( "int fun(){ return 42; }")
fun()
42
Kod C++ w code.cpp
#include <Rcpp.h>
using namespace Rcpp ;
// [[Rcpp::export]]
int timesTwo( int x ){
return 2*x ;
}
Funkcja sourceCpp() kompiluje i wczytuje plik
library(Rcpp)
sourceCpp( "code.cpp" )
timesTwo( 21 )
42
#include <Rcpp.h>
_____ _________ ____ _
__ ________________
___ _________ ___ _ __
______ ___ _
_
Należy dołączać tylko Rcpp.h
#include <Rcpp.h>
using namespace Rcpp ;
__ ________________
___ _________ ___ _ __
______ ___ _
_
Something zamiast Rcpp::Something, gdy Something należy do Rcpp#include <Rcpp.h>
using namespace Rcpp ;
// [[Rcpp::export]]
___ _________ ___ _ __
______ ___ _
_
#include <Rcpp.h>
using namespace Rcpp ;
// [[Rcpp::export]]
int timesTwo( int x ){
return 2*x ;
}
#include <Rcpp.h>
using namespace Rcpp ;
// [[Rcpp::export]]
int timesTwo( int x ){
return 2*x ;}
Wczytanie funkcji do R
library(Rcpp)
sourceCpp( "code.cpp" )
Wywołanie jak każdej innej funkcji R.
timesTwo( 21 )
42
Optymalizacja kodu R za pomocą Rcpp