标题说明了一切。我试图寻找这个,但无济于事。
基本上,我正在运行一个 1000 次迭代的 For 循环,我想编写一个函数,每 10 次迭代显示/打印模拟的进程。
最佳答案
R 内置的进度条怎么样?它在控制台上打印一个进度条,让您通过迭代监控进度。不幸的是,这并不能完全回答这个问题,因为它在循环的每一轮中都会更新,如果事先不知道迭代次数,它并不能完全让您到达您想要的位置。进度条的工作原理如下:
# Number of iterations
imax<-c(10)
# Initiate the bar
pb <- txtProgressBar(min = 0, max = imax, style = 3)
# Iterations
for(i in 1:imax) {
Sys.sleep(1) # Put here your real simulation
# Update the progress bar
setTxtProgressBar(pb, i)
}
# Finally get a new line on the console
cat("\n")
你当然可以用模数来完成你所关心的事情。下面是 for 循环的一个例子:
for(i in 1:1000) {
# Modulus operation
if(i %% 10==0) {
# Print on the screen some message
cat(paste0("iteration: ", i, "\n"))
}
Sys.sleep(0.1) # Just for waiting a bit in this example
}
这些中的任何一个对你有用吗?
关于r - 每 10 次迭代打印 1000 次迭代的进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26729674/