Rcpp ile R Kodunu Optimize Etme
Romain François
Consulting Datactive, ThinkR
#include <Rcpp.h>
using namespace Rcpp ;
int twice( int x ){
return 2*x ;
}
// [[Rcpp::export]]
int universal(){
return twice(21) ;
}
R'den çağırma:
# Olmaz, twice içsel
twice(21)
Error in twice(21) : could not find function "twice"
# Sorun yok
universal()
42
Satır sonuna kadar yorum:
// Bir yorum
Birden çok satıra yayılan yorumlar
/*
Birden uzun yorum
birden çok satır sürer
bazen bir kod parçasını karantinaya almak için kullanılır,
örneğin
int x = 42 ;
*/
int x = 38 ;
#include <Rcpp.h>
using namespace Rcpp ;
int twice( int x ){
return 2*x ;}
// [[Rcpp::export]]
int universal(){
return twice(21) ;}
/*** R
# Bu R kodu
12 + 30
# `universal` fonksiyonunu çağırma
universal()
*/
if( condition ){
// koşul doğruysa kod
} else {
// değilse kod
}
// [[Rcpp::export]]
void info( double x){
if( x < 0 ){
Rprintf( "x is negative" ) ;
} else if( x == 0 ){
Rprintf( "x is zero" ) ;
} else if( x > 0 ){
Rprintf( "x is positive" ) ;
} else {
Rprintf( "x is not a number" ) ;
}
}
info(-2) info(0)
x is negative x is zero
info(3) info(NaN)
x is positive x is not a number
Rcpp ile R Kodunu Optimize Etme