重新启动时发生错误循环

重新启动时发生错误循环

问题描述:

以下示例不执行所需的操作。即错误时,循环不会重新启动,而是运行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) 

这个怎么样。我保存原始的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