[R] R-Embedding and error messages

Luke Tierney luke at stat.uiowa.edu
Wed Aug 20 20:25:59 CEST 2008


On Wed, 20 Aug 2008, Martin Morgan wrote:

> Hi Jorge --
>
> I hesitate to suggest this unsupported solution (as these public
> suggestions tend to mean that the solution disappears!), but in C

Don't use this -- it is subject to change/removal with no notice.

I do not believe there is a C level methanism for catching the R error
conditon at this point.  Thinking about this is on the list but it
hasn't happened yet.  One thing you can do is use the geterrmessage R
function to retrieve the most recent message (so if R_tryEval
indicates an error occurred then build and execute a call to
geterrmessage and extranct the message from the returned STRSXP).

luke

> extern void R_SetErrorHook(void (*)(SEXP, char *));
>
> void
> my_ErrorHook(SEXP call, char *message)
> {
>
>   /*
>        etc; e.g., cache message, perhaps reset error hook (hook is
>        removed after error; nested R-level try() may mean multiple
>        errors per R_tryEval?
>   */
>
> }
>
> and then
>
> ...
>
>    R_SetErrorHook( my_ErrorHook );
>    PROTECT(value = R_tryEval( expr, env, errorOccurred ));
>    if( *errorOccurred ) {
>        /* etc, e.g., retrieve error message(s) from cache */
>    }
>    /*
>        hmm, maybe R_SetErrorHook needs to be cleared here, or at
>        least replaced with a no-op?
>    */
> ...
>
> This is an R-devel question, not R-help.
>
> Martin
>
> "Jorge W. Cardoso" <cardoso at montevideo.com.uy> writes:
>
>> I'm writing a C++ application using R-embedding to do some
>> forecast process.
>>
>> I also use R_tryEval instead of R_Eval to run my R-script,
>> so that in case of error I know exactly in which line number
>> was the last error.
>>
>> In particular, from time in time y get some error messages
>> refering an exceptional condition in the arima model.
>>
>> I want to catch these error messages from de C code
>> (or at least the code error number) to take some
>> corrective actions.
>>
>> Is there any way to do that ?
>>
>> Regards,
>>
>> Jorge W. Cardoso
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>

-- 
Luke Tierney
Chair, Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu



More information about the R-help mailing list