C++ işlevleri C++ dosyalarına aittir

Rcpp ile R Kodunu Optimize Etme

Romain François

Consulting Datactive, ThinkR

Bu derste, daha önce

evalCpp()

evalCpp( "40 + 2" )
42

cppFunction()

cppFunction( "int fun(){ return 42; }")
fun()
42
Rcpp ile R Kodunu Optimize Etme

C++ kodu code.cpp içinde

#include <Rcpp.h>
using namespace Rcpp ; 
// [[Rcpp::export]]
int timesTwo( int x ){
  return 2*x ; 
}

sourceCpp() işlevi bunu derler ve yükler

library(Rcpp)

sourceCpp( "code.cpp" )
timesTwo( 21 )
42
Rcpp ile R Kodunu Optimize Etme

Rcpp başlık dosyasını dahil et

#include <Rcpp.h>
_____ _________ ____ _

__ ________________
___ _________ ___ _ __
  ______ ___ _
_
  • Yalnızca Rcpp.h dosyasını dahil et

    • Diğer başlık dosyalarını otomatik olarak içerir
Rcpp ile R Kodunu Optimize Etme

Rcpp ad alanını kullanma

#include <Rcpp.h>
using namespace Rcpp ;

__ ________________
___ _________ ___ _ __
  ______ ___ _
_
  • Something, Rcpp::Something yerine, Something Rcpp içindeyse kullan
Rcpp ile R Kodunu Optimize Etme

İşlevi R'ye aktarma

#include <Rcpp.h>
using namespace Rcpp ;

// [[Rcpp::export]]
___ _________ ___ _ __
  ______ ___ _
_
Rcpp ile R Kodunu Optimize Etme

İşlevin kendisi

#include <Rcpp.h>
using namespace Rcpp ;

// [[Rcpp::export]]
int timesTwo( int x ){
  return 2*x ;
}
Rcpp ile R Kodunu Optimize Etme
#include <Rcpp.h>
using namespace Rcpp ;

// [[Rcpp::export]]
int timesTwo( int x ){
  return 2*x ;}

işlevi R'ye yükle

library(Rcpp)
sourceCpp( "code.cpp" )

Diğer R işlevleri gibi çağır.

timesTwo( 21 )
42
Rcpp ile R Kodunu Optimize Etme

Hadi pratik yapalım!

Rcpp ile R Kodunu Optimize Etme

Preparing Video For Download...