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
AKSHAY M KULKARNI
[[alternative HTML version deleted]]
I might try something like this:
FUN1 <- function ()
{
threshold <- 4L
fails <- 0L
internal <- function() {
## do the actual downloading here
tryCatch({
download.file(<...>)
}, error = function() {
fails <<- fails + 1L
if (fails >= threshold) stop("unable to download
file(s)")
internal()
})
}
internal()
}
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 at 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
> AKSHAY M KULKARNI
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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]]
Dear Iris,
THanks a lot...
Thanking you,
Yours sincerely,
AKSHAY M KULKARNI
________________________________
From: Iris Simmons <ikwsimmo at gmail.com>
Sent: Wednesday, April 19, 2023 11:22 PM
To: akshay kulkarni <akshay_e4 at hotmail.com>
Cc: R help Mailing list <r-help at 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
tryCatch({
download.file(<...>)
}, error = function() {
fails <<- fails + 1L
if (fails >= threshold) stop("unable to download
file(s)")
internal()
})
}
internal()
}
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 at
hotmail.com<mailto:akshay_e4 at 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
AKSHAY M KULKARNI
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org<mailto:R-help at 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]]