Dear David, 

Once again, thank you very much for your email. 
I believe I understand why I got a different number of "warnings”.

It seems that when you type "length(warnings)" you keep the information on the 
run before the last.
I come up with a simple example called "testing" (you will need to install the 
library “rootSolve” if you want to run the function). The code is at the end of 
the email.
If you open R, without loading any previous workspaces, and run the function, 
you will get

________________________
> source("testing.R")
> testing(100)

 lw= 0 
There were 100 or more warnings (use warnings() to see the first 100)
________________________

If you repeat but with a different number of iterations, e.g., 200 then you get

________________________
> testing(200)

 lw= 100 
There were 200 or more warnings (use warnings() to see the first 200)
________________________


So it seems that the number of warnings is only updated once the function stops 
running and R “returns” to the prompt. This does not work for me because I need 
to take into account the number of failures/warnings (this is an important 
information that I need to compute and take into account in the output of the 
main function).

I realize this is likely to be a rather “beginner’s question", but if there is 
a way of storing the number of warnings while running the function, I would 
appreciate you let me know. 

Best, 

Luís

testing <- function(nrep){

 

   options(nwarnings =nrep)

   library("rootSolve")

 

   for(i in 1:nrep){

      (ss <- multiroot(f = model, start = c(1, 1, 1)))

   }

 

   lw <- length(warnings())

   cat("\n first lw=", lw,"\n")

}

 

model <- function(x){

 

   F1 <- runif(1)*x[1] + runif(1)*x[2] + runif(1)*x[3]^2 -12

   F2 <- runif(1)*x[1]^2 - runif(1)*x[2] + runif(1)*x[3] -2

   F3 <- runif(1)*2*x[1] - runif(1)*x[2]^2 + runif(1)*x[3] -1

 

   c(F1 = F1, F2 = F2, F3 = F3)

}

_______________________________
Luís Borda de Água
Centro de Biologia Ambiental
Faculdade de Ciências
Universidade de Lisboa
Edifício C2, 6º Piso, Sala 2.6.04/07 
Campo Grande
1749-016 Lisboa, Portugal
Tel: +351 21 750 00 00 (ext: 22607)
Fax: +351 21 750 00 28


        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to