Andreas Wittmann
2008-Dec-07 07:38 UTC
[R] Finding the first value without warning in a loop
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
David Winsemius
2008-Dec-07 16:27 UTC
[R] Finding the first value without warning in a loop
Have you considered instead sending the warnings to someplace where it won't bother the user? If so then sink() might be useful. My reading of the help page suggests that your call to suppressWarnigs is storing any results outside the Global environment. This is what I came up with. It may not be as general as you may want, since it only detects one kind of warned event, a NaN result. testit <- function(a) { options(warn=-1) repeat {z=log(a) a=a+ 0.1 if(!is.nan(z) || a > 5 ) {options(warn=0); return(a); break } } } > testit(-3) [1] 0.1 -- David Winsemius On Dec 7, 2008, at 2:38 AM, Andreas Wittmann 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 at 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.
Gabor Grothendieck
2008-Dec-07 17:21 UTC
[R] Finding the first value without warning in a loop
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 <andreas_wittmann at gmx.de> 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 at 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. >
David Winsemius
2008-Dec-07 18:09 UTC
[R] Finding the first value without warning in a loop
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 > <andreas_wittmann at gmx.de> 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 at 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 at 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.
Gabor Grothendieck
2008-Dec-07 18:14 UTC
[R] Finding the first value without warning in a loop
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 <dwinsemius at comcast.net> 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 >> <andreas_wittmann at gmx.de> 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 at 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 at 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. > >
Possibly Parallel Threads
- capture.output(eval(..., envir)) not evaluate in the expected(?) environment
- Missing objects using dump.frames for post-mortem debugging of crashed batch jobs. Bug or gap in documentation?
- Missing objects using dump.frames for post-mortem debugging of crashed batch jobs. Bug or gap in documentation?
- issue with using rm: cannot generate on-the-fly list
- enty-wise closest element