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

Iris Simmons |kw@|mmo @end|ng |rom gm@||@com
Wed Apr 19 19:52:45 CEST 2023

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
attempts. I hope this helps!

On Wed, Apr 19, 2023, 12:57 akshay kulkarni <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 mailing list -- To UNSUBSCRIBE and more, see
> 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.

	[[alternative HTML version deleted]]

More information about the R-help mailing list