[R] Testing for warning inside functions
Peter Wolf
s-plus at wiwi.uni-bielefeld.de
Wed May 18 12:03:26 CEST 2005
Prof Brian Ripley wrote:
> On Wed, 18 May 2005, Peter Wolf wrote:
>
>> I am looking for a way to get a warning message
>> immediately after an evaluation within a function.
>> To get error messages you can use geterrmessage().
>
>
> Well, only in an error handler, as an error normally throws you out of
> the function. (That is the point of geterrmessage(), to enable error
> handlers to be written.)
>
> To print the message immediately, look up options("warn").
options(warn=1) prints the message immediately,
but I cannot store it in a variable to analyse it or to show the message
it in a message box, for example.
> We don't have a general warnings handler mechanism, but you can make
> use of condition objects.
Thank you, I will have a look at condition objects.
>> But I found no function that allows me to check for
>> warnings.
>>
>> Five years ago this questions has been posted
>> but I haven't found any answer.
>
>
> What exactly is `this question'? I see several assertions but no
> question. (It would have been helpful to give an exact reference to
> the archives.)
link to the old mail: R-help archive Jan - June 2000:
* [R] Testing for warning inside functions -- V/idhyanath Rao (Sun 07
May 2000 - 12:46:38 EST)/
Message-ID: <000301bfb831$33b87c60$d370fe8c at lucent.com>
"This is probably a newbie question: How do I test, inside a function, is
a call I made from inside that function produced a warning? ..."
>
>
>> Thanks for any help.
>>
>> Peter Wolf
>>
>>
>> ------------
>>
>> For illustration purpose a simple example follows:
>> you can get warnings after an error occured or after
>> exiting from the function but not at once after
>> "x<-matrix(1:3,2,2)":
>>
>>> options(warn=0)
>>> f<-function(x){
>>
>> x<-matrix(1:3,2,2)
>> print(0)
>> warnings() # no warning is shown
>> print(1)
>> try({xxx})
>> print(2)
>> print(geterrmessage())
>> print(3)
>> warnings()
>> print(4)
>> }
>>
>>> f()
>>
>> [1] 0
>> [1] 1
>> Fehler in try({ : Objekt "xxx" nicht gefunden
>> Zusätzlich: Warnmeldung:
>> Datenlänge [3] ist kein Teiler oder Vielfaches der Anzahl der Zeilen
>> [2] in matrix
>> [1] 2
>> [1] "Fehler in try({ : Objekt \"xxx\" nicht gefunden\n"
>> [1] 3
>> Warning message:
>> Datenlänge [3] ist kein Teiler oder Vielfaches der Anzahl der Zeilen
>> [2] in matrix
>> [1] 4
>
>
More information about the R-help
mailing list