2011-12-08 25 views
7

Estoy intentando informar errores de mi código rcpp. Estoy usando el constructor exception (const char *message_, const char *file, int line) de http://dirk.eddelbuettel.com/code/rcpp/html/classRcpp_1_1exception.html. Para aislar el problema, me escribió lo siguiente bar.cpp:Planteando excepciones en Rcpp

#include <Rcpp.h> 

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

Cuando lo ejecuto en R, esto es lo que me sale:

> 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' 
> 

Respuesta

5

Usted puede

  • utilizar el inline paquete que pone un bloque try/catch en la función que genera para usted (mediante el uso de dos macros simples)

  • o hacerlo de forma manual a sí mismo como se muestra en un montón de ejemplos en mi blog, o en los ejemplos/sección del paquete Rcpp,

pero haciendo lo que usted (es decir: tirar fuera de un intento/catch block) nunca puede funcionar.

Como un beneficio adicional, aquí es un ejemplo completo (que esencialmente ya existía en las pruebas de unidad):

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> 

Una vez más, cxxfunction() pone un bloque try/catch() aquí para usted como se puede ver si gire verbose en:

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> 

el BEGIN_RCPP y END_CPP añadir la magia que necesita aquí.

Por favor haga mueva sus preguntas a rcpp-devel.

+0

Gracias! de los ejemplos, ¿quiere decir 'copyMessageToR' y' Rf_error' como se usa en RcppDateExample.cpp? No pude encontrar la documentación para 'copyMessageToR'. – highBandWidth

+0

Aah ... pero preguntar aquí parece funcionar bien, dando excelentes respuestas en minutos, ¡después de todo! ¿Me entiendes? ;) –

+0

Sí. Eso es lo que usas en el bloque 'catch()'. –

3

Simplemente envuelva el código dentro de BEGIN_RCPP/END_RCPP:

RcppExport SEXP bar(SEXP x){ 
BEGIN_RCPP 

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

END_RCPP 
} 

Tenga en cuenta que usted puede lanzar excepciones std normales también:

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

Incluso después de agregar 'BEGIN_RCPP' y' END_RCPP', 'Rcpp :: exception' todavía da el mismo error' STL_VECTOR_ELT'. 'std :: invalid_argument' arroja una excepción, pero el mensaje que se muestra en R es un' Error genérico: excepción C++ (razón desconocida) '. – highBandWidth