R:for 循环中的文本进度条 [英] R: Text progress bar in for loop

查看:43
本文介绍了R:for 循环中的文本进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些示例代码,其中包含一个 for 循环并创建了一些这样的图(我的实际数据创建了数千个图):

I have some sample code which contains a for loop and creates some plots like this (my actual data creates several thousands of plots):

xy <- structure(list(NAME = structure(c(2L, 3L, 1L, 1L), .Label = c("CISCO","JOHN", "STEPH"), class = "factor"), ID = c(41L, 49L, 87L, 87L), X_START_YEAR = c(1965L, 1948L, 1959L, 2003L), Y_START_VALUE = c(940L,-1760L, 110L, 866L), X_END_YEAR = c(2005L, 2000L, 2000L, 2007L), Y_END_VALUE = c(940L, -1760L, 110L, 866L), LC = structure(c(1L,1L, 2L, 2L), .Label = c("CA", "US"), class = "factor")), .Names = c("NAME", "ID", "X_START_YEAR", "Y_START_VALUE", "X_END_YEAR", "Y_END_VALUE","LC"), class = "data.frame", row.names = c(NA, -4L))

ind <- split(xy,xy$ID) # split by ID for different plots

# Plots
for (i in ind){
  xx = unlist(i[,grep('X_',colnames(i))])
  yy = unlist(i[,grep('Y_',colnames(i))])    
  fname <- paste0(i[1, 'ID'],'.png')
  png(fname, width=1679, height=1165, res=150)
  par(mar=c(6,8,6,5))
  plot(xx,yy,type='n',main=unique(i[,1]), xlab="Time [Years]", ylab="Value [mm]") 
  i <- i[,-1]
  segments(i[,2],i[,3],i[,4],i[,5],lwd=2)
  points(xx, yy, pch=21, bg='white', cex=0.8)
  dev.off()
} 

要查看 for 循环的进度,我有兴趣将进度条合并到我的代码中.正如我从 R 文档中发现的那样,有 txtProgressBar http://stat.ethz.ch/R-manual/R-patched/library/utils/html/txtProgressBar.html从该页面的示例中,我了解到您必须将 for 循环写入一个函数中才能在之后调用它,而我正在为我的示例而苦苦挣扎.

To see the progress of the for loop I would be interested to incorporate a progress bar to my code. As I found from the R documentation there is the txtProgressBar http://stat.ethz.ch/R-manual/R-patched/library/utils/html/txtProgressBar.html From the example of that page I understand that you have to write the for loop into a function to call it afterwards which I am struggling with my example.

如何在 for 循环中实现进度条?

How could I implement a progress bar into the for loop?

推荐答案

要使进度条正常工作,您需要一个数字来跟踪进度.这是作为一般规则的原因之一,我更喜欢使用 (i in 1:length(ind)) 而不是直接将我想要的对象放在那里.或者,您只需创建另一个 stepi 变量,您在每次迭代中都执行 stepi = stepi + 1.

for progress bar to work you need a number to track your progress. that is one of the reasons as a general rule I prefer using for with (i in 1:length(ind)) instead of directly putting the object I want there. Alternatively you'll just create another stepi variable that you do stepi = stepi + 1 in every iteration.

首先需要在循环外创建progressbar对象

you first need to create the progressbar object outside the loop

pb = txtProgressBar(min = 0, max = length(ind), initial = 0) 

然后你需要在每次迭代中更新

then inside you need to update with every iteration

setTxtProgressBar(pb,stepi)

setTxtProgressBar(pb,i)

记得关闭进度条输出换行符.来自文档:

Remember to close the progress bar to output the newline character. From the documentation:

完成后应该关闭进度条:这会输出最后一个换行符.

The progress bar should be closed when finished with: this outputs the final newline character.

只需在循环末尾添加:

close(pb)

如果循环中也有 print 命令,这将无法正常工作

This will work poorly if the loop also has print commands in it

这篇关于R:for 循环中的文本进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆