log(-1) is not an error in R bug it does generate a warning and a NaN result.
If there is a warning then the statement that causes the warning and all remaining statements in the tryCatch are not executed. Instead the warning= function is invoked. On Sun, Dec 7, 2008 at 1:09 PM, David Winsemius <[EMAIL PROTECTED]> wrote: > Thanks for that. I am having some difficulty figuring out how it works. The > help page tells me (I think) that "y<-log(x)" gets matched to expr. Am I > correct in thinking that if either an error or warning is produced, that > tryCatch silently returns <something> (or <nothing>?) to the for loop, and > only otherwise goes on to evaluate the rest of the second assignment and > break call? So any warning would abort processing of the remaining > expressions? > > Are the assignment "xx<-x" and the break call considered "handlers" as > described in the help page? > > I see the purpose of the last argument. Taking out the warning = > function(w{}) changes the behavior when none of the elements in the vector > passed to x were valid. > >> for (x in c(-2, -1)) { > + tryCatch({ > + y <- log(x) > + xx <- x > + break > + }) > + } > Warning message: > In log(x) : NaNs produced >> print(xx) > [1] -2 > > > -- > > David Winsemius > > On Dec 7, 2008, at 12:21 PM, Gabor Grothendieck wrote: > >> Try this: >> >> for (x in c(-2, 2, 4)) { >> tryCatch({ >> y <- log(x) >> xx <- x >> break >> }, warning = function(w) {}) >> } >> print(xx) # 2 >> >> >> >> On Sun, Dec 7, 2008 at 2:38 AM, Andreas Wittmann >> <[EMAIL PROTECTED]> wrote: >>> >>> Dear R useRs, >>> >>> with the following piece of code i try to find the first value which can >>> be >>> calculated without warnings >>> >>> `test` <- function(a) >>> { >>> repeat >>> { >>> ## hide warnings >>> suppressWarnings(log(a)) >>> >>> if (exists("last.warning", envir = .GlobalEnv)) >>> { >>> a <- a + 0.1 >>> ## clear existing warnings >>> rm("last.warning", envir = .GlobalEnv) >>> } >>> >>> if(a > 5 || !exists("last.warning", envir = .GlobalEnv)) >>> break >>> } >>> >>> return(a) >>> } >>> >>> if i run this with test(-3), i would expect a=0 as return value. >>> Is it also possible to hide warnings during my function, i guess i use >>> suppressWarnings in a wrong way here? >>> >>> Thanks and best regards >>> >>> Andreas >>> >>> ______________________________________________ >>> 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. >>> >> >> ______________________________________________ >> 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. > > ______________________________________________ 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.