0

I created a loop to download and take statistic data from jpeg files. Some of the links are not working. Is there any option in R to remove them from dataset or skip them in loop?

im using jpeg package to download files.

1 Answers1

2

Yes. This is a typical programming problem: you need to perform some tasks, but you know that some of them may fail. However, sometimes you cannot determine which tasks will fail and when will they fail.

To get around this, a solution is to use some form of error handling. This makes sense when you can determine what to do if a specific task fails. In your case, you only want to skip a task if the first part of it fails, right?

In R, there's a structure called tryCatch, which, as the name suggests, tries an expression and has defined behavior for an occasional error. You can find an example of that structure here.

If we were to apply that structure here, I think the following should help:

number_of_tasks <- 100

for (task in 1:number_of_tasks) {
    my_image <- tryCatch(
        {
            download_jpeg_image(task) # You should put your code here!
        },
        error = function(cond) {
            print("Something went wrong when downloading jpeg image...")
            # we won't do anything else here!
        }
    )
}

eduardokapp
  • 1,612
  • 1
  • 6
  • 28