R/progress.R
progress.Rd
Display progression level of a long-running task in the console. Two mode can be used: either percent of achievement (55\ items or steps done on a total (1 file on 10 done...). This is displayed either through a message, or through a text-based "progression bar" on the console, or a true progression bar widget in a GUI.
progress(
value,
max.value = NULL,
progress.bar = FALSE,
char = "|",
init = (value == 0),
console = TRUE,
gui = TRUE
)
Current value of the progression (use a value higher than
max.value
to dismiss the progression indication automatically.
Maximum value to be achieved.
Should we display a progression bar on the console? If
FALSE
, a temporary message is used.
The character to use to fill the progress bar in the console. not used for the alternate display, or for GUI display of progression.
Do we have to initialize the progress bar? It is usually done the
first time the function is used, and the default value init = (value == 0)
is correct most of the time. You must specify init = TRUE
in two cases:
(1) if the first value to display is different from zero, and (2) if your
code issues some text on screen during progression display. Hence, you must
force redraw of the progression bar.
Do we display progression on the console?
Do we display progression in a dialog box, or any other GUI widget? See "details" and "examples" hereunder to know how to implement your own GUI progression indicator.
This function returns NULL
invisibly. It is invoked for its side
effects.
The function progress()
proposes different styles of progression
indicators than the standard txtProgressBar()
in package 'utils'.
The function uses backspace (\8) to erase characters at the console.
With gui = TRUE
, it looks for all functions defined in the .progress
list
located in the SciViews:TempEnv
environment. Each function is executed in
turn with following call: the_gui_function(value, max.value)
. You are
responsible to create the_gui_function()
and to add it as an element in
the .progress
list. See also example (5) hereunder.
If your GUI display of the progression offers the possibility to stop calculation (for instance, using a 'Cancel' button), you are responsible to pass this info to your code doing the long calculation and to stop it there. Example (5) shows how to do this.
# 1) A simple progress indicator in percent
for (i in 0:101) {
progress(i)
Sys.sleep(0.01)
if (i == 101) message("Done!")
}
#> Progress: 0% Progress: 1% Progress: 2% Progress: 3% Progress: 4% Progress: 5% Progress: 6% Progress: 7% Progress: 8% Progress: 9% Progress: 10% Progress: 11% Progress: 12% Progress: 13% Progress: 14% Progress: 15% Progress: 16% Progress: 17% Progress: 18% Progress: 19% Progress: 20% Progress: 21% Progress: 22% Progress: 23% Progress: 24% Progress: 25% Progress: 26% Progress: 27% Progress: 28% Progress: 29% Progress: 30% Progress: 31% Progress: 32% Progress: 33% Progress: 34% Progress: 35% Progress: 36% Progress: 37% Progress: 38% Progress: 39% Progress: 40% Progress: 41% Progress: 42% Progress: 43% Progress: 44% Progress: 45% Progress: 46% Progress: 47% Progress: 48% Progress: 49% Progress: 50% Progress: 51% Progress: 52% Progress: 53% Progress: 54% Progress: 55% Progress: 56% Progress: 57% Progress: 58% Progress: 59% Progress: 60% Progress: 61% Progress: 62% Progress: 63% Progress: 64% Progress: 65% Progress: 66% Progress: 67% Progress: 68% Progress: 69% Progress: 70% Progress: 71% Progress: 72% Progress: 73% Progress: 74% Progress: 75% Progress: 76% Progress: 77% Progress: 78% Progress: 79% Progress: 80% Progress: 81% Progress: 82% Progress: 83% Progress: 84% Progress: 85% Progress: 86% Progress: 87% Progress: 88% Progress: 89% Progress: 90% Progress: 91% Progress: 92% Progress: 93% Progress: 94% Progress: 95% Progress: 96% Progress: 97% Progress: 98% Progress: 99% Progress: 100%
#> Done!
if (FALSE) { # \dontrun{
# 2) A progress indicator with 'x on y'
for (i in 0:31) {
progress(i, 30)
Sys.sleep(0.02)
if (i == 31) message("Done!")
}
# 3) A progress bar in percent
for (i in 0:101) {
progress(i, progress.bar = TRUE)
Sys.sleep(0.01)
if (i == 101) message("Done!")
}
# 4) A progress indicator with 'x on y'
for (i in 0:21) {
progress(i, 20, progress.bar = TRUE)
Sys.sleep(0.03)
if (i == 21) message("Done!")
}
} # }
# 5) A progression dialog box with Tcl/Tk
if (FALSE) { # \dontrun{
if (require(tcltk)) {
gui_progress <- function(value, max.value) {
# Do we need to destroy the progression dialog box?
if (value > max.value) {
try(tkdestroy(get_temp("gui_progress_window")), silent = TRUE)
delete_temp(c("gui_progress_state", "gui_progress_window",
"gui_progress_cancel"))
return(invisible(FALSE))
} else if (exists_temp("gui_progress_window") &&
!inherits(try(tkwm.deiconify(tt <- get_temp("gui_progress_window")),
silent = TRUE), "try-error")) {
# The progression dialog box exists
# Focus on it and change progress value
tkfocus(tt)
state <- get_temp("gui_progress_state")
tclvalue(state) <- value
} else {
# The progression dialog box must be (re)created
# First, make sure there is no remaining "gui_progress_cancel"
delete_temp("gui_progress_cancel")
# Create a Tcl variable to hold current progression state
state <- tclVar(value)
assign_temp("gui_progress_state", state)
# Create the progression dialog box
tt <- tktoplevel()
assign_temp("gui_progress_window", tt)
tktitle(tt) <- "Waiting..."
sc <- tkscale(tt, orient = "h", state = "disabled", to = max.value,
label = "Progress:", length = 200, variable = state)
tkpack(sc)
but <- tkbutton(tt, text = "Cancel", command = function() {
# Set a flag telling to stop running calculation
assign_temp("gui_progress_cancel", TRUE) # Content is not important!
tkdestroy(tt)
})
tkpack(but)
}
invisible(TRUE)
}
# Register it as function to use in progress()
change_temp(".progress", "gui_progress", gui_progress,
replace.existing = TRUE)
rm(gui_progress) # Don't need this any more
# Test it...
for (i in 0:101) {
progress(i) # Could also set console = FALSE for using the GUI only
Sys.sleep(0.05)
# The code to stop long calc when user presses "Cancel"
if (exists_temp("gui_progress_cancel")) {
progress(101, console = FALSE) # Make sure to clean up everything
break
}
if (i == 101) message("Done!")
}
# Unregister the GUI for progress
change_temp(".progress", "gui_progress", NULL)
}
} # }