0

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: withTimeout: Evaluate an R expression and interrupts it if it takes too long, but I am not sure if this is the correct function to use for such a task.

What can be recommended and how can I to use it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
stats_noob
  • 5,401
  • 4
  • 27
  • 83
  • Does this answer your question? [Time out an R command via something like try()](https://stackoverflow.com/questions/7891073/time-out-an-r-command-via-something-like-try) – Matt Summersgill Jun 30 '23 at 20:02

1 Answers1

2

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

bretauv
  • 7,756
  • 2
  • 20
  • 57