我想应用一个函数(这个解释为"foo")将数据向量转换为另一个值.此函数将数据作为输入,并需要将表单提交到网页.有时候,这很快就会发生,而其他时候,这可能需要很长时间.我想以跳过花费太长时间的项目的方式运行for循环(或等效的apply函数).我尝试使用以下内容限制循环运行的时间,然后跳到下一个5秒:
pb <- txtProgressBar(min = 1, max = 100, style = 3)
storage <- matrix(nrow = sample.length, ncol = 2)
for(i in 1:100){
s <- Sys.time()
storage[i,] <- try(foo(data.vec[i]), TRUE)
if (Sys.time() - s >5) {next}
# update progress bar
setTxtProgressBar(pb, i)
}
close(pb)
Run Code Online (Sandbox Code Playgroud)
我认为我不能理解如何在for循环中应用'next'条件.我们已经寻找更清楚的解释,但这里没有运气.
我正在尝试让一个函数运行指定的时间,目前我正在尝试使用该system.time函数。我不知道如何定义一个新变量来获取函数运行的累积值,然后将其放入 while 循环中。
timer<-(system.time(simulated_results<-replicate(n=1,simulation(J,10000,FALSE,0.1),simplify="vector"))[3])
print(timer)
while(cumsum(timer)<15){
print(cumsum(timer))
simulated_results<-replicate(n=10000,simulation(J,10000,FALSE,0.1),simplify="vector")
}
Run Code Online (Sandbox Code Playgroud)
我将非常感谢任何帮助!