Retry for-loop R loop if error

前端 未结 1 1274
梦毁少年i
梦毁少年i 2021-01-01 06:31

I have an R For loop that downloads data from a server and adds result to table however, I sometimes get an error stopping the loop. If I tell it to redo the last download a

相关标签:
1条回答
  • 2021-01-01 06:43

    You could throw a try-catch combo.

    for (i in 1:1000){
        while(TRUE){
           df <- try(downloadfnc("URL", file = i), silent=TRUE)
           if(!is(df, 'try-error')) break
        }
        table[i,] <- df
    }
    

    This will continue within the while loop until the file is successfully downloaded, and only move on when it is successfully downloaded.

    0 讨论(0)
提交回复
热议问题