[R] capturing warnings within loops, so I know the iterations where warnings occurred?

Anika Masters anika.masters at gmail.com
Tue Sep 24 00:36:25 CEST 2013


I am running a loop.  Warnings sometimes occur, and the lop continues
until the end.
For each iteration of the loop, I wish to capture and "save" any
warnings issued, so that I can tell on which iteration the warnings
were issued.
I tried this, but it does not work.

mylist <- list(NULL)
mylist_warns <- list(NULL)
old.warn <- options(warn=1)

x <- c(1:5)
for (i in 1:2) {
assign("last.warning", NULL, envir = baseenv())
mylist[[i]] = x[ (i:1): 5 ]
mylist_warns[[i]] = warnings()
}

mylist
mylist_warns



More information about the R-help mailing list