2017-02-27 92 views
0

以下示例不執行所需的操作。即錯誤時,循環不會重新啓動,而是運行1000(或提供的任何長度)並停止。我需要它重新啓動每個10sec並從錯誤開始重新開始計算。重新啓動時發生錯誤循環

下面是摘錄:

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) 

回答

1

這個怎麼樣。我保存原始的n,然後將內部的n重置爲錯誤的原點

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) 
+0

謝謝。我會測試它2mrw並讓你知道。 – Maximilian

+0

它現在按預期工作,但是,由於某種原因,我現在無法通過* .bat批處理文件命令終止我的R會話,強制關閉R會話。好像R被凍結或者忙碌......我不知道如何解決這個問題......謝謝 – Maximilian