[R] tryCatch - Continuing for/next loop after error

Jonathan P Daily jdaily at usgs.gov
Thu Mar 10 17:43:50 CET 2011


I did not know that. When reading the help topic for the first time 
myself, I think I assumed that it returned no value since it had no Value 
section, and I haven't used it in a way that it would return a value.
--------------------------------------
Jonathan P. Daily
Technician - USGS Leetown Science Center
11649 Leetown Road
Kearneysville WV, 25430
(304) 724-4480
"Is the room still a room when its empty? Does the room,
 the thing itself have purpose? Or do we, what's the word... imbue it."
     - Jubal Early, Firefly

"William Dunlap" <wdunlap at tibco.com> wrote on 03/10/2011 11:29:59 AM:

> [image removed] 
> 
> RE: [R] tryCatch - Continuing for/next loop after error
> 
> William Dunlap 
> 
> to:
> 
> Jonathan P Daily, Nipesh Bajaj
> 
> 03/10/2011 11:30 AM
> 
> Cc:
> 
> r-help
> 
> > -----Original Message-----
> > From: r-help-bounces at r-project.org 
> > [mailto:r-help-bounces at r-project.org] On Behalf Of Jonathan P Daily
> > Sent: Thursday, March 10, 2011 7:49 AM
> > To: Nipesh Bajaj
> > Cc: r-help at r-project.org
> > Subject: Re: [R] tryCatch - Continuing for/next loop after error
> > 
> > Wow, I had a major brain fart there. The function after error 
> > was supposed 
> > to accept an argument, and next won't do anything there 
> > because inside the 
> > function there is no loop. Also, the first argument of tryCatch is an 
> > expression, so do the assignment there. Corrections inline below:
> 
> The stuff about the error handle requiring an argument and
> not being able to call next() from outside of a loop is true,
> but you do not need to do the assignment inside the main expression
> of tryCatch().  I find it clearer if you do the assignment outside
> of tryCatch() so the main expression can return one thing and the
> error handler can return something else.  You can use this in an
> *apply() function or a for loop, while the assignment-in-tryCatch()
> won't work in a *apply() call.  E.g., suppose we have a function
> that only works on positive odd integers:
>   o <- function (x) {
>       stopifnot(x%%2 == 1, x>0)
>       (x - 1)/2
>   }
> Then you can use tryCatch to flag the bad inputs with a code
> that tells you about the bad input:
>   > sapply(1:5, function(i)tryCatch(o(i), error=function(e)-1000-i))
>   [1]     0 -1002     1 -1004     2
> or with a for loop where you don't have to prefill the output
> vector with any particular values:
>   > value <- numeric(5)
>   > for(i in 1:5) value[i] <- tryCatch(o(i), error=function(e)-1000-i)
>   > value
>   [1]     0 -1002     1 -1004     2
> 
> To do that with with an assignment in the main expression of tryCatch
> would require something like
>   > value <- -1000 -(1:5)
>   > for(i in 1:5) tryCatch(value[i] <- o(i), error=function(e)NULL)
>   > value
>   [1]     0 -1002     1 -1004     2
> which seems to me uglier and harder to follow since value's entries
> are filled in different parts of the code.
> 
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com 
> > --------------------------------------
> > Jonathan P. Daily
> > Technician - USGS Leetown Science Center
> > 11649 Leetown Road
> > Kearneysville WV, 25430
> > (304) 724-4480
> > "Is the room still a room when its empty? Does the room,
> >  the thing itself have purpose? Or do we, what's the word... 
> > imbue it."
> >      - Jubal Early, Firefly
> > 
> > Nipesh Bajaj <bajaj141003 at gmail.com> wrote on 03/10/2011 10:21:38 AM:
> > 
> > > [image removed] 
> > > 
> > > Re: [R] tryCatch - Continuing for/next loop after error
> > > 
> > > Nipesh Bajaj 
> > > 
> > > to:
> > > 
> > > Jonathan P Daily
> > > 
> > > 03/10/2011 10:21 AM
> > > 
> > > Cc:
> > > 
> > > r-help
> > > 
> > > Hi Jonathan, I was also trying to understand this tryCatch 
> > function on
> > > my own problem. Here is mine:
> > > 
> > > fn1 <- function(x) {
> > >       if(as.integer(x) == 5) {
> > >             stop("stop") 
> > >          }
> > >       return(x+5)
> > >    }
> > > res <- matrix(NA, 5, 7)
> > > for(i in 1:5) {
> > >       for(j in 1:7) {
> > >             res[i,j] <- tryCatch(fn1(j), error = function() next)
> > >          }
> > >    }
> > 
> > for(i in 1:5) { for(j in 1:7) {
> >         tryCatch(res[i,j] <- fn1(j), error = function(x) x)
> > } }
> > 
> > > 
> > > I was expecting while completion of these loops, the 5th column of
> > > 'res' will have NA values. However I got some error:
> > > 
> > > Error in value[[3L]](cond) : unused argument(s) (cond)
> > > 
> > > What is the actual problem here?
> > > 
> > > Thanks,
> > > 
> > > 
> > > On Thu, Mar 10, 2011 at 7:19 PM, Jonathan P Daily <jdaily at usgs.gov> 
> > wrote:
> > > > --------------------------------------
> > > > Jonathan P. Daily
> > > > Technician - USGS Leetown Science Center
> > > > 11649 Leetown Road
> > > > Kearneysville WV, 25430
> > > > (304) 724-4480
> > > > "Is the room still a room when its empty? Does the room,
> > > >  the thing itself have purpose? Or do we, what's the 
> > word... imbue 
> > it."
> > > >     - Jubal Early, Firefly
> > > >
> > > > r-help-bounces at r-project.org wrote on 03/10/2011 03:51:15 AM:
> > > >
> > > >> [image removed]
> > > >>
> > > >> [R] tryCatch - Continuing for/next loop after error
> > > >>
> > > >> Costas
> > > >>
> > > >> to:
> > > >>
> > > >> r-help
> > > >>
> > > >> 03/10/2011 03:53 AM
> > > >>
> > > >> Sent by:
> > > >>
> > > >> r-help-bounces at r-project.org
> > > >>
> > > >> Please respond to costas.vorlow
> > > >>
> > > >> Dear all,
> > > >>
> > > >> I am not sure I understand fully the functionality of 
> > "tryCatch" and
> > > >> "try" commands and how these are used to continue a 
> > for/next loop if 
> > an
> > > >> error occurs within the loop.
> > > >>
> > > >> Can somebody point me to material (or share some code) with more
> > > >> extensive examples than the ones in the help/FAQ pages?
> > > >>
> > > >> Do explain my problem in more detail:
> > > >>
> > > >> for (i in 100:1000)
> > > >> {
> > > >>       ## do some other stuff
> > > >>
> > > >>      dataset<-head(data,i)
> > > >>      tryCatch(estimatemodel(dataset))
> > > >
> > > > if estimatemodel returns an error (i.e. via a call to 
> > stop()), then 
> > this
> > > > should break out of the loop:
> > > >
> > > > tryCatch(estimatemodel(dataset), error = function() break)
> > > >
> > > > if you want to skip to the next iteration, use:
> > > > tryCatch(estimatemodel(dataset), error = function() next)
> > > >
> > > >>
> > > >> }
> > > >>
> > > >> My for/next loop reads in data (increasing the dataset 
> > by one point 
> > at
> > > >> every loop run) and then estimates a model. When the problem is
> > > >> computationally singular, the loop exits. I want to 
> > continue the loop
> > > >> and register an "error" estimation value for that step. 
> > However when 
> > I
> > > >> use use the try tryCatch(estimatemodel(data)) (where 
> > estimatemodel() 
> > is
> > > >> a wrapper function calling the model estimation and optimization
> > > >> routines), the problem still persists.
> > > >>
> > > >> Is this the correct way to use tryCatch (or try) or 
> > should these go
> > > >> inside the actual code bits (i.e., in a more low level 
> > fashion) that
> > > >> conduct the optimization and model estimation?
> > > >>
> > > >> Apologies if this is not clear enough.
> > > >>
> > > >> Best,
> > > >> Costas
> > > >>
> > > >> ______________________________________________
> > > >> 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.
> > > >
> > 
> > ______________________________________________
> > 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.
> >



More information about the R-help mailing list