Execute an Rscript until it has finished successfully [duplicate] - r

How can I simply tell R to retry a statement a few times if it errors? E.g. I was hoping to do something like:
tryCatch(dbGetQuery(...), # Query database
error = function(e) {
if (is.locking.error(e)) # If database is momentarily locked
retry(times = 3) # retry dbGetQuery(...) 3 more times
else {
# Handle other errors
}
}
)

I usually put the try block in a loop,
and exit the loop when it no longer fails or the maximum number of attempts is reached.
some_function_that_may_fail <- function() {
if( runif(1) < .5 ) stop()
return(1)
}
r <- NULL
attempt <- 1
while( is.null(r) && attempt <= 3 ) {
attempt <- attempt + 1
try(
r <- some_function_that_may_fail()
)
}

I wrote a quick function that allows you to easily retry an operating a configurable number of times, with a configurable wait between attempts:
library(futile.logger)
library(utils)
retry <- function(expr, isError=function(x) "try-error" %in% class(x), maxErrors=5, sleep=0) {
attempts = 0
retval = try(eval(expr))
while (isError(retval)) {
attempts = attempts + 1
if (attempts >= maxErrors) {
msg = sprintf("retry: too many retries [[%s]]", capture.output(str(retval)))
flog.fatal(msg)
stop(msg)
} else {
msg = sprintf("retry: error in attempt %i/%i [[%s]]", attempts, maxErrors,
capture.output(str(retval)))
flog.error(msg)
warning(msg)
}
if (sleep > 0) Sys.sleep(sleep)
retval = try(eval(expr))
}
return(retval)
}
So you can just write val = retry(func_that_might_fail(param1, param2), maxErrors=10, sleep=2) to retry calling that function with those parameters, give up after 10 errors, and sleep 2 seconds between attempts.
Also, you can redefine the meaning of what an error looks like by passing a different function as parameter isError, which by default will catch an error signaled with stop. This is useful if the function being called does something else on error, such as returning FALSE or NULL.
This is the alternative I've found so far that results in clearer, more readable code.
Hope this helps.

A solution without pre-assigning values and using for instead of while:
some_function_that_may_fail <- function(i) {
if( runif(1) < .5 ) stop()
return(i)
}
for(i in 1:10){
try({
r <- some_function_that_may_fail(i)
break #break/exit the for-loop
}, silent = FALSE)
}
r will be equal to the number of tries that were needed. If you dont want the output of the errors set silent to TRUE

Here's a function to generate a custom condition to respond to
locked <- function(message="occurred", ...) {
cond <- simpleCondition(message, ...)
class(cond) <- c("locked", class(cond))
cond
}
and a function implemented to allow (an infinite number of) restarts
f <- function() {
cnt <- 0L
repeat {
again <- FALSE
cnt <- cnt + 1L
withRestarts({
## do work here, and if needed...
signalCondition(locked())
}, retry=function() {
again <<- TRUE
})
if (!again) break
}
cnt
}
and the use of withCallingHandlers (to keep the context where the condition was signaled active unlike tryCatch) to handle the locked condition
withCallingHandlers({
n_tries <- 0L
f()
}, locked=function(e) {
n_tries <<- n_tries + 1L
if (n_retries < 3)
invokeRestart("retry")
})

I have put together the code and make it package: retry
f <- function(x) {
if (runif(1) < 0.9) {
stop("random error")
}
x + 1
}
# keep retring when there is a random error
retry::retry(f(1), when = "random error")
#> [1] 2
# keep retring until a requirement is satisified.
retry::retry(f(1), until = function(val, cnd) val == 2)
#> [1] 2
# or using one sided formula
retry::retry(f(1), until = ~ . == 2, max_tries = 10)
#> [1] 2

I like setting my object as an error from the start, also sometimes useful to add some sleep time if you're having connection problems:
res <- simpleError("Fake error to start")
counter <- 1
max_tries <- 10
# Sys.sleep(2*counter)
while(inherits(res, "error") & counter < max_tries) {
res <- tryCatch({ your_fun(...) },
error = function(e) e)
counter <- counter + 1
}

Related

skip an iteration in for-loop in case of an error message

I am writing a code to scrape data from websites in R where I'm bound to get an error saying no such directory was found.
It is a part of a longer for-loop and in case of error when the loop usually ends itself, I wish to continue the loop but instead, the iteration should move to the next count.
Code:
library(geniusr)
lyrics_list = list(length = nrow(artists))
for(i in 1:50)
{
counter = 1
for(j in 1:row(artist_list[[i]]))
{
if(error == TRUE)
{
counter = j+1
next
}
lyrics_list[[i]][[counter]] <- list(get_lyrics_search(artist_name = artist_list[[i]][counter,1], song_title = artist_list[[i]][counter,30]))
counter = counter + 1
}
}
This was my earlier approach before I tried my hands with tryCatch()
library(geniusr)
lyrics_list = list(length = nrow(artists))
for(i in 1:50)
{
counter = 1
for(j in 1:nrow(artist_list[[i]]))
{
tryCatch({lyrics_list[[i]][[counter]] <- list(get_lyrics_search(artist_name = artist_list[[i]][counter,1], song_title = artist_list[[i]][counter,30]))
counter = counter + 1})
}
}
The expected error message which I'll encounter in the loop is:
Error in open.connection(x, "rb") : HTTP error 404.
In case of the error, I want to move to the next iteration, i.e., increase j or increase I if that's the case
All suggestions for the if() conditional part will be welcomed
Try replacing your trycatch with the fol­lowing:
​tryCatch({ ​ ​ ​ lyrics_list­[[i]][[counter]] <- list(get_lyrics_sear­ch(artist_name = art­ist_list[[i]][counte­r,1], song_title = artist_list[[i]][coun­ter,30])) ​ ​ counter = coun­ter + 1 ​ ​ }, error = fun­ction(e) {conditionM­essage(e) } ​ ​ )
This is what my professor suggested!

Getting "unexpected numeric constant" in code

I am trying to run my code but get the following error:
Error: unexpected numeric constant in: "
if((final.string[1+loop.check]) !=
(final.string[string.length-loop.check])){
return FALSE"
My code is as below:
inputString = "aabb"
string.length <- nchar(inputString)
compare.list <- strsplit(inputString,"")
final.string <- unlist(compare.list)
loop.check <- 0L
if(string.length %% 2 == 0){
loop.stop <- string.length/2
}
if(string.length %% 2 == 1){
loop.stop <- (string.length -1) / 2
}
while (loop.check<=loop.stop){
if((final.string[1+loop.check]) != (final.string[string.length-loop.check])){
return FALSE
break
}
else{
loop.check <- loop.check + 1
}
}
if(loop.check-1==loop.stop){
return TRUE
}
If I run just the (final.string[1+loop.check]) != (final.string[string.length-loop.check]) portion then the console returns FALSE so the code for the if statement condition seems to be working. However, when attempting to run the whole script I get the above error.

Restart for loop on error

The below example does not do what is required. Namely, on error the loop does not restart, but rather runs the 1000 (or what ever length is provided) and stops. I would need it to restart every 10sec and restart the calculation from beginning on error.
Here is snippet:
get_daydata <- function(n){
message(paste("remaining runs", n))
withRestarts(err <- tryCatch({ for(i in seq_along(he)){
#.... some calculation .....
}},error=function(e) { invokeRestart("rerun") }),
rerun = function() { message ("re-running"); stopifnot(n > 0);
for(i in 1:10) { Sys.sleep(1); cat(i) }; getdata(n-1) })}
get_daydata(1000)
How about this. I save the original n and then reset the inner n to orig_n on error
get_daydata <- function(n) {
orig_n <- n
message(paste("remaining runs", n))
withRestarts(
err <- tryCatch({
for (i in seq_along(he)) {
#.... some calculation .....
}
}, error = function(e) {
n <<- orig_n + 1
invokeRestart("rerun")
}),
rerun = function() {
message ("re-running")
stopifnot(n > 0)
for (i in 1:2) {
Sys.sleep(1)
cat(i)
}
get_daydata(n - 1)
}
)
}
get_daydata(1000)

How to restart a loop with eval with timeout in R?

while (!exists("j")) {
i <- 1
repeat {
tryCatch(expr = {
print(i)
raw.result <- evalWithTimeout(Sys.sleep(i), timeout = 3)
if (i == 1) {
j <- i
} else {
j <- c(j, i)
}
i <- i + 1
}, TimeoutException = function(ex) {
rm("j")
})
}
}
The above code is getting stuck at i=4 and keeps executing the function for i=4, however I want it to restart from i=1, whenever there is an error.
Can someone please guide on where am I doing it wrong?
In your codeTimeoutException is unable to find j as it is evaluated in a different environment. Even if it was able to find it, nothing would change. As tryCatch is stopping an error from breaking a repeat loop, thus repeat will continue with the current i. You could explicitly break out from the repeat, but in that case you have deleted j, thus your while will stop.
I am not quite sure why you need while loop here.
Here is a modification of your code that will work as you want.
Fist explicitly set i <- 1, and rest it again to i <<-1 (Note <<- as i is one environment above tryCatch).
i <- 1
repeat {
tryCatch(
expr = {
print(i)
raw.result <- R.utils:evalWithTimeout(Sys.sleep(i), timeout = 3)
if (i == 1) {
j <- i
} else {
j <- c(j, i)
}
i <- i + 1
},
TimeoutException = function(ex) {
i <<- 1
}
)
}

How to retry a statement on error?

How can I simply tell R to retry a statement a few times if it errors? E.g. I was hoping to do something like:
tryCatch(dbGetQuery(...), # Query database
error = function(e) {
if (is.locking.error(e)) # If database is momentarily locked
retry(times = 3) # retry dbGetQuery(...) 3 more times
else {
# Handle other errors
}
}
)
I usually put the try block in a loop,
and exit the loop when it no longer fails or the maximum number of attempts is reached.
some_function_that_may_fail <- function() {
if( runif(1) < .5 ) stop()
return(1)
}
r <- NULL
attempt <- 1
while( is.null(r) && attempt <= 3 ) {
attempt <- attempt + 1
try(
r <- some_function_that_may_fail()
)
}
I wrote a quick function that allows you to easily retry an operating a configurable number of times, with a configurable wait between attempts:
library(futile.logger)
library(utils)
retry <- function(expr, isError=function(x) "try-error" %in% class(x), maxErrors=5, sleep=0) {
attempts = 0
retval = try(eval(expr))
while (isError(retval)) {
attempts = attempts + 1
if (attempts >= maxErrors) {
msg = sprintf("retry: too many retries [[%s]]", capture.output(str(retval)))
flog.fatal(msg)
stop(msg)
} else {
msg = sprintf("retry: error in attempt %i/%i [[%s]]", attempts, maxErrors,
capture.output(str(retval)))
flog.error(msg)
warning(msg)
}
if (sleep > 0) Sys.sleep(sleep)
retval = try(eval(expr))
}
return(retval)
}
So you can just write val = retry(func_that_might_fail(param1, param2), maxErrors=10, sleep=2) to retry calling that function with those parameters, give up after 10 errors, and sleep 2 seconds between attempts.
Also, you can redefine the meaning of what an error looks like by passing a different function as parameter isError, which by default will catch an error signaled with stop. This is useful if the function being called does something else on error, such as returning FALSE or NULL.
This is the alternative I've found so far that results in clearer, more readable code.
Hope this helps.
A solution without pre-assigning values and using for instead of while:
some_function_that_may_fail <- function(i) {
if( runif(1) < .5 ) stop()
return(i)
}
for(i in 1:10){
try({
r <- some_function_that_may_fail(i)
break #break/exit the for-loop
}, silent = FALSE)
}
r will be equal to the number of tries that were needed. If you dont want the output of the errors set silent to TRUE
Here's a function to generate a custom condition to respond to
locked <- function(message="occurred", ...) {
cond <- simpleCondition(message, ...)
class(cond) <- c("locked", class(cond))
cond
}
and a function implemented to allow (an infinite number of) restarts
f <- function() {
cnt <- 0L
repeat {
again <- FALSE
cnt <- cnt + 1L
withRestarts({
## do work here, and if needed...
signalCondition(locked())
}, retry=function() {
again <<- TRUE
})
if (!again) break
}
cnt
}
and the use of withCallingHandlers (to keep the context where the condition was signaled active unlike tryCatch) to handle the locked condition
withCallingHandlers({
n_tries <- 0L
f()
}, locked=function(e) {
n_tries <<- n_tries + 1L
if (n_retries < 3)
invokeRestart("retry")
})
I have put together the code and make it package: retry
f <- function(x) {
if (runif(1) < 0.9) {
stop("random error")
}
x + 1
}
# keep retring when there is a random error
retry::retry(f(1), when = "random error")
#> [1] 2
# keep retring until a requirement is satisified.
retry::retry(f(1), until = function(val, cnd) val == 2)
#> [1] 2
# or using one sided formula
retry::retry(f(1), until = ~ . == 2, max_tries = 10)
#> [1] 2
I like setting my object as an error from the start, also sometimes useful to add some sleep time if you're having connection problems:
res <- simpleError("Fake error to start")
counter <- 1
max_tries <- 10
# Sys.sleep(2*counter)
while(inherits(res, "error") & counter < max_tries) {
res <- tryCatch({ your_fun(...) },
error = function(e) e)
counter <- counter + 1
}

Resources