Sunday, 15 January 2012

function - R: Text progress bar in for loop -



function - R: Text progress bar in for loop -

i have sample code contains loop , creates plots (my actual info 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 id different plots # plots (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[,-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 progress of loop interested incorporate progress bar code. found r documentation there txtprogressbar http://stat.ethz.ch/r-manual/r-patched/library/utils/html/txtprogressbar.html illustration of page understand have write loop function phone call afterwards struggling example.

how implement progress bar loop?

for progress bar work need number track progress. 1 of reasons general rule prefer using (i in 1:length(ind)) instead of straight putting object want there. alternatively you'll create stepi variable stepi = stepi + 1 in every iteration.

you first need create progressbar object outside loop

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

then within need update every iteration

settxtprogressbar(pb,stepi)

or

settxtprogressbar(pb,i)

this work poorly if loop has print commands in it

r function for-loop progress-bar

No comments:

Post a Comment