2011-12-08 18 views
7

私のrcppコードからエラーを報告しようとしています。私はhttp://dirk.eddelbuettel.com/code/rcpp/html/classRcpp_1_1exception.htmlからコンストラクタexception (const char *message_, const char *file, int line)を使用しています。問題を特定するために、私は次のbar.cppを書いた:Rcppで例外を発生させる

#include <Rcpp.h> 

RcppExport SEXP bar(SEXP x){ 
     throw(Rcpp::exception("My Error Message","bar.cpp",4)); 
     return x ; 
} 

私はRでそれを実行すると、これは私が得るものです:

> dyn.load("bar.so") 
> is.loaded("bar") 
[1] TRUE 
> .Call("bar",12) 
Error: SET_VECTOR_ELT() can only be applied to a 'list', not a 'NULL' 
> 

答えて

5

あなたは

  • を使用しますかinlinetry/catchブロックを(2つの単純なマクロを使用して)生成する関数にブロックします

  • または手動で自分で私のブログ上の例の束に示すように、またはRcppパッケージの例/セクションでそれを行う、

います(つまり、何をやって:試してみるの外に投げます/ catchブロック)は動作しません。追加ボーナスとして

、ここでは(基本的にすでにユニットテストに存在していた)完全な例である:あなたがあれば見ることができるように再び

R> library(inline) 
R> f <- cxxfunction(signature(), plugin="Rcpp", body=' 
+ throw std::range_error("boom"); 
+ return R_NilValue; 
+ ') 
R> f() 
Error in f() : boom 
R> 

cxxfunction()はあなたのためにここにブロックtry/catch()を置きますあなたがここに必要な魔法を追加

R> f <- cxxfunction(signature(), plugin="Rcpp", body=' 
+ throw std::range_error("boom"); 
+ return R_NilValue; 
+ ', verbose=TRUE) 
>> setting environment variables: 
PKG_LIBS = -L/usr/local/lib/R/site-library/Rcpp/lib \ 
      -lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib 

>> LinkingTo : Rcpp 
CLINK_CPPFLAGS = -I"/usr/local/lib/R/site-library/Rcpp/include" 

>> Program source : 

    1 : 
    2 : // includes from the plugin 
    3 : 
    4 : #include <Rcpp.h> 
    5 : 
    6 : 
    7 : #ifndef BEGIN_RCPP 
    8 : #define BEGIN_RCPP 
    9 : #endif 
    10 : 
    11 : #ifndef END_RCPP 
    12 : #define END_RCPP 
    13 : #endif 
    14 : 
    15 : using namespace Rcpp; 
    16 : 
    17 : 
    18 : 
    19 : // user includes 
    20 : 
    21 : 
    22 : // declarations 
    23 : extern "C" { 
    24 : SEXP file4cc53282() ; 
    25 : } 
    26 : 
    27 : // definition 
    28 : 
    29 : SEXP file4cc53282(){ 
    30 : BEGIN_RCPP 
    31 : 
    32 : throw std::range_error("boom"); 
    33 : return R_NilValue; 
    34 : 
    35 : END_RCPP 
    36 : } 
    37 : 
    38 : 
Compilation argument: 
/usr/lib/R/bin/R CMD SHLIB file4cc53282.cpp 2> file4cc53282.cpp.err.txt 
ccache g++-4.6 -I/usr/share/R/include \ 
    -I"/usr/local/lib/R/site-library/Rcpp/include" \ 
    -fpic -g0 -O3 -Wall -pipe -Wno-unused -pedantic -c file4cc53282.cpp \ 
    -o file4cc53282.o 
g++ -shared -o file4cc53282.so file4cc53282.o \ 
    -L/usr/local/lib/R/site-library/Rcpp/lib \ 
    -lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib \ 
    -L/usr/lib/R/lib -lR 
R> 

BEGIN_RCPPEND_CPP:あなたは上verboseをオンにします。

を入力してください質問をrcpp-develに転送してください。

+0

をありがとう!例から、RcppDateExample.cppで使われている 'copyMessageToR'と' Rf_error'を意味しますか?私は 'copyMessageToR'のドキュメントを見つけることができませんでした。 – highBandWidth

+0

ああ...しかし、ここに尋ねるだけでうまくいくように見え、結局数分で素晴らしい答えが得られるようです!私のドリフトをキャッチ? ;) –

+0

はい。これは 'catch()'ブロックで使用するものです。 –

3

だけBEGIN_RCPP/END_RCPP内のコードをラップ:あなたはあまりにも通常のSTDの例外を投げることができる

RcppExport SEXP bar(SEXP x){ 
BEGIN_RCPP 

     throw(Rcpp::exception("My Error Message","bar.cpp",4)); 
     return x ; 

END_RCPP 
} 

注:

throw std::invalid_argument("'x' is too short"); 
+0

'BEGIN_RCPP'と' END_RCPP'を追加した後でも、 'Rcpp :: exception'は引き続き同じ' STL_VECTOR_ELT'エラーを返します。 'std :: invalid_argument'は例外をスローしますが、Rに表示されるメッセージは一般的な' Error:C++ exception(unknown reason) 'です。 – highBandWidth