Otimizando código R com 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) ;
}
Chamando do R:
# Não é possível: twice é interna
twice(21)
Error in twice(21) : could not find function "twice"
# Funciona
universal()
42
Comentário até o fim da linha:
// Um comentário
Comentários em múltiplas linhas
/*
Um comentário maior
que ocupa várias linhas
às vezes usado para isolar um trecho de código,
por exemplo
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
# Este é código R
12 + 30
# Chamando a função `universal`
universal()
*/
if( condition ){
// code if true
} else {
// code otherwise
}
// [[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
Otimizando código R com Rcpp