디버깅

Rcpp로 R 코드 최적화하기

Romain François

Consulting Datactive, ThinkR

콘솔에 출력하기

Rprintf()를 사용하여 콘솔에 메시지를 출력하는 함수

cppFunction( '
int fun(){  

    // Some values
    int x = 42 ;

    // Printing a message to the R console
    Rprintf( "some message in the console, x=%d\\n", x ) ;

    // Return some int
    return 76 ;
}
')
Rcpp로 R 코드 최적화하기

Rprintf와 플레이스홀더

%d를 사용한 정수 플레이스홀더

int x = 42 ;

Rprintf( "some message in the console, x=%d\n", x ) ;

// Prints the following in the console: 
some message in the console, x=42

%s를 사용한 문자열 플레이스홀더

Rprintf( "roses are %s, violets are %s\n", "red", "blue" ) ;

// Prints the following:
roses are red, violets are blue
Rcpp로 R 코드 최적화하기

Rprintf()를 사용하여 콘솔에 메시지를 출력하는 함수

cppFunction( 'int fun(){  
    // some values
    int x = 42 ;

    // printing a message to the R console
    Rprintf( "some message in the console, x=%d\\n", x ) ;

    // return some int
    return 76 ;}     ')

함수 호출

fun()
some message in the console, x=42
76
Rcpp로 R 코드 최적화하기

오류 메시지

0에서 20 사이의 숫자만 허용하는 함수

cppFunction( 'int fun(int x){  
    // A simple error message
    if( x < 0 ) stop( "sorry x should be positive" ) ;    
    // A formatted error message 
    if( x > 20 ) stop( "x is too big (x=%d)", x ) ;    
    // Return some int
    return x ;     }')
fun(-2)
Error in fun(-2) : sorry x should be positive
Rcpp로 R 코드 최적화하기

오류 메시지

fun(23)
Error in fun(23) : x is too big (x=23)
tryCatch( fun(24), error = function(e){
    message("C++ exception caught: ", conditionMessage(e))
})
C++ exception caught: x is too big (x=24)
Rcpp로 R 코드 최적화하기

연습해 봅시다!

Rcpp로 R 코드 최적화하기

Preparing Video For Download...