Rcpp で R コードを最適化する
Romain François
Consulting Datactive, ThinkR
evalCpp()
evalCpp( "40 + 2" )
42
cppFunction()
cppFunction( "int fun(){ return 42; }")
fun()
42
code.cppのC++コード
#include <Rcpp.h>
using namespace Rcpp ;
// [[Rcpp::export]]
int timesTwo( int x ){
return 2*x ;
}
sourceCpp()関数でコンパイル・読み込みを行う
library(Rcpp)
sourceCpp( "code.cpp" )
timesTwo( 21 )
42
#include <Rcpp.h>
_____ _________ ____ _
__ ________________
___ _________ ___ _ __
______ ___ _
_
Rcpp.hのみインクルードする
#include <Rcpp.h>
using namespace Rcpp ;
__ ________________
___ _________ ___ _ __
______ ___ _
_
SomethingがRcppに含まれる場合、Rcpp::Somethingの代わりにSomethingを使用する#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 ;}
関数をRに読み込む
library(Rcpp)
sourceCpp( "code.cpp" )
他のR関数と同様に呼び出す。
timesTwo( 21 )
42
Rcpp で R コードを最適化する