Optimiser le code R avec Rcpp
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) ;
}
Appels depuis R :
# Impossible : twice est interne
twice(21)
Error in twice(21) : could not find function "twice"
# OK
universal()
42
Commenter jusqu'à la fin de la ligne :
// Un commentaire
Commentaires sur plusieurs lignes
/*
Un commentaire plus long
sur plusieurs lignes
parfois utilisé pour mettre en quarantaine du code,
par exemple
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
# Ceci est du code R
12 + 30
# Appel de la fonction `universal`
universal()
*/
if( condition ){
// code si vrai
} else {
// sinon
}
// [[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
Optimiser le code R avec Rcpp