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

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

问题描述

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

 <$结构(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, (1L,1L,2L,2L),.Label = c(CA,US),等级= 2000L,2007L),Y_END_VALUE = c(940L,-1760L,110L,866L) factor)),.Names = c(NAME,ID,X_START_YEAR,Y_START_VALUE,X_END_YEAR,Y_END_VALUE,LC),class =data.frame名称= c(NA,-4L))

ind < - split(xy,xy $ ID)#根据ID分为不同的地块

#地块
(for ind){
xx = unlist(i [,grep('X _',colnames(i))])
yy = unlist(i [,grep('Y _',colnames (i))])
fname< - paste0(i [1,'ID'],'。png')
png(fname,width = 167 9,height = 1165,res = 150)
par(mar = c(6,8,6,5))
plot(xx,yy,type ='n',main = unique [,1]),xlab =Time [Years],ylab =Value [mm])
i < - i [, - 1]
segments(i [,2],i [,3],i [,4],i [,5],lwd = 2)
点(xx,yy,pch = 21,bg ='white',cex = 0.8) .off()
}

要查看for循环的进度,将进度条合并到我的代码中。正如我从R文档中发现的 txtProgressBar http://stat.ethz.ch/R-manual/R-patched/library/utils/html/txtProgressBar.html
从这个页面的例子我明白,你必须写for循环到函数来调用它,我正在努力与我的例子。



我怎么能实现一个进度bar进入for循环?

解决方案

为了进度条的工作,你需要一个数字来跟踪你的进度。这是我喜欢用(i in 1:length(ind))的一般规则的原因之一,而不是直接把我想要的对象放在那里。或者,你可以在每一次迭代中创建另外一个 stepi 变量,你可以做 stepi = stepi + 1 。 >

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

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

每次迭代更新

  setTxtProgressBar(pb,stepi)



$ p $ $ $ $ $ $ $ setTxtProgressBar(pb,i)
code>

如果循环中 print 命令


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()
} 

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.

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

解决方案

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.

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)

or

setTxtProgressBar(pb,i)

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

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

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