[R] Testing for warning inside functions

Peter Wolf s-plus at wiwi.uni-bielefeld.de
Wed May 18 10:51:53 CEST 2005


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().
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.

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
 > version
platform i686-pc-linux-gnu
arch     i686
os       linux-gnu
system   i686, linux-gnu
status
major    2
minor    1.0
year     2005
month    04
day      18
language R




More information about the R-help mailing list