[R] running a function repeatedly on error....

akshay kulkarni @k@h@y_e4 @end|ng |rom hotm@||@com
Wed Apr 19 21:03:53 CEST 2023

Dear Iris,
                THanks a lot...

Thanking you,
Yours sincerely,
From: Iris Simmons <ikwsimmo using gmail.com>
Sent: Wednesday, April 19, 2023 11:22 PM
To: akshay kulkarni <akshay_e4 using hotmail.com>
Cc: R help Mailing list <r-help using r-project.org>
Subject: Re: [R] running a function repeatedly on error....

I might try something like this:

FUN1 <- function ()
    threshold <- 4L
    fails <- 0L
    internal <- function() {
        ## do the actual downloading here
        }, error = function() {
            fails <<- fails + 1L
            if (fails >= threshold) stop("unable to download file(s)")

which should attempt to download the files, stopping after 4 failed attempt[[elided Hotmail spam]]

On Wed, Apr 19, 2023, 12:57 akshay kulkarni <akshay_e4 using hotmail.com<mailto:akshay_e4 using hotmail.com>> wrote:
Dear members,
                          I have a function FUN1 that downloads some data from the internet. It so happens that the function doesn't work the first time, but on the second or third attempt it works. I want to run the function repeatedly for four times if it throws an error:

X <- tryCatch(FUN1, error = function(c) {FUN1})

This runs the function two times. But I want to run the function four times if throws an error, but on the fifth attempt if it throws an error, abort. I know I can include the tryCatch call inside FUN1 and call it, but any short and elegant code to that effect?

Thanking you,
Yours sincerely

        [[alternative HTML version deleted]]

R-help using r-project.org<mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]

More information about the R-help mailing list