Home > Enterprise >  Telling a Loop to "Skip" an Iteration if it Takes Too Long
Telling a Loop to "Skip" an Iteration if it Takes Too Long

Time:09-17

I have this code in R:

output = list()

for (i in 1:999) 

{tryCatch({

{

    link_i<-  paste0(www.some_website, i 1,  /some_extension/, i,  .com)

    material_i <-  fromJSON(link_i)





   output[[i]] <- material_i

}

   }, error = function(e){})

}

Due to the nature of the code I am running, I have noticed that sometimes this loop gets "stuck" on a specific iteration. For instance, this loop might get stuck on the 45th iteration and take a very long time.

I am looking for some mechanism to tell the computer that "if more than x seconds is spent on a certain iteration, skip to the next iteration".

I found this function over here that might be useful : https://www.rdocumentation.org/packages/R.utils/versions/2.11.0/topics/withTimeout - but I am not sure if this is the correct function to use for such a task.

Can someone please recommend something and please show me how to use it?

Thank you!

CodePudding user response:

The function you linked to seems quite appropriate to me (but I've never used it). Here's an example with a function foo() that is quite fast for all inputs except when x = 5 where it takes 5 seconds to run.

suppressPackageStartupMessages(library(R.utils))

# function that is fast except when x = 5
foo <- function(x) {
  if (x == 5) {
    Sys.sleep(5)
  } else {
    Sys.sleep(0.5)
  }
  return(x)
}

# create a list to store the results
res <- vector("list", length = 10L)

# Make a loop where we try to store the result of each iteration. If iteration
# takes more than 1 sec, then skip it.
for (i in 1:10) {
  print(i)
  tryCatch({
    res[[i]] <- withTimeout({
      foo(i)
    }, timeout = 1)
  }, TimeoutException = function(ex) {
    message("Timeout. Skipping.")
    res[[i]] <- NULL
  })
}
#> [1] 1
#> [1] 2
#> [1] 3
#> [1] 4
#> [1] 5
#> Timeout. Skipping.
#> [1] 6
#> [1] 7
#> [1] 8
#> [1] 9
#> [1] 10

res
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
#> [1] 3
#> 
#> [[4]]
#> [1] 4
#> 
#> [[5]]
#> NULL
#> 
#> [[6]]
#> [1] 6
#> 
#> [[7]]
#> [1] 7
#> 
#> [[8]]
#> [1] 8
#> 
#> [[9]]
#> [1] 9
#> 
#> [[10]]
#> [1] 10

Created on 2022-09-16 with reprex v2.0.2

  •  Tags:  
  • r
  • Related