【发布时间】:2015-01-11 05:48:42
【问题描述】:
我有一些示例代码,其中包含一个 for 循环并创建了一些像这样的图(我的实际数据创建了数千个图):
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 循环写入一个函数,然后再调用它,我正在努力解决我的示例。
如何在 for 循环中实现进度条?
【问题讨论】:
标签: r function for-loop progress-bar