1 printing for-loop r progress
标题说明了一切.我试着寻找这个,但无济于事.
基本上,我正在运行一个1000次迭代的For循环,我想编写一个函数,每10次迭代显示/打印模拟的进度.
小智 8
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")
Run Code Online (Sandbox Code Playgroud)
你当然可以用模数来完成你正在寻找的东西.这是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
}
Run Code Online (Sandbox Code Playgroud)
这些中的任何一个都适合你吗?
| 归档时间: |
|
| 查看次数: |
8121 次 |
| 最近记录: |