R/progressBar.R

Defines functions pbClose pbStep pbCreate

Documented in pbClose pbCreate pbStep

# Author: Robert J. Hijmans
# Date: Sept 2008
# Version 0.9
# Licence GPL v3


pbCreate <- function(nsteps, progress, style=3, label='Progress', ...) {

	if (missing(progress)) {
		progress <- .progress()
	}

	if (is.null(progress)) {
		progress <- .progress()
	}
	
	if (progress=='text') {
		pb <- utils::txtProgressBar(min=0, max=nsteps, style=style)
	} else if (progress %in% c('window', 'tcltk', 'windows')) {
		tit <- paste(label, ' (', nsteps, ' steps)', sep='')
		#if (.Platform$OS.type == "windows" ) {
		#	pb <- winProgressBar(title=tit, min=0 , max=nsteps, width = 300, label='starting')
		#} else {
		requireNamespace("tcltk")
		pb <- tcltk::tkProgressBar(title=tit, min=0, max=nsteps, width = 300, label='starting')
		#}
	} else {
		pb <- 'none'
	}
	attr(pb, "starttime") <- Sys.time()
	return(pb)
}



pbStep <- function(pb, step=NULL, label='') {
	if (inherits(pb, "txtProgressBar")) {
		if (is.null(step)) { 
			step = pb$getVal() + 1 
		}
		utils::setTxtProgressBar(pb, step)
	} else if (inherits(pb, "tkProgressBar")) {
		if (is.null(step)) { 
			step = pb$getVal() + 1 
		}
		tcltk::setTkProgressBar(pb, step, label=paste(label, step))	
	}
	#} else if (pbclass=="winProgressBar") {
	#	if (is.null(step)) { step <- getWinProgressBar(pb)+1  }
	#	setWinProgressBar(pb, step, label=paste(label, step))	
	#} 
}

pbClose <- function(pb, timer) {
	if (inherits(pb, "txtProgressBar")) {
		cat("\n\r")
		close(pb)
	} else if (inherits(pb, "tkProgressBar")) {
		close(pb)
	}
	if (missing(timer)) {
		timer <- .timer()		
	}
	if (timer) {
		elapsed <- difftime(Sys.time(), attr(pb, "starttime"), units = "secs")
		cat(round(as.numeric(elapsed)), 'seconds\n')
	}
}

Try the raster package in your browser

Any scripts or data that you put into this service are public.

raster documentation built on Oct. 14, 2023, 5:07 p.m.