Package: R.utils
Class TextStatusBar

Object
~~|
~~+--TextStatusBar

Directly known subclasses:

public static class TextStatusBar
extends Object

A status bar at the R prompt that can be updated.

TextStatusBar(fmt=paste("%-", getOption("width") - 1, "s", sep = ""), ...)

Arguments

fmt

A character format string to be used by sprintf(). Default is a left-aligned string of full width.

...

Named arguments to be passed to sprintf() together with the format string.

Fields and Methods

Methods:

flushFlushes the output.
getLabelGets the current value of a label.
newlineWrites a newline.
popMessageAdds a message above the status bar.
setLabelSets the value of a label.
setLabelsSets new values of given labels.
updateUpdates the status bar (visually).
updateLabelsSets the new values of given labels and updates the status bar.

Methods inherited from Object:
$, $<-, [[, [[<-, as.character, attach, attachLocally, clearCache, clearLookupCache, clone, detach, equals, extend, finalize, getEnvironment, getFieldModifier, getFieldModifiers, getFields, getInstantiationTime, getStaticInstance, hasField, hashCode, ll, load, names, objectSize, print, save

Details

A label with name hfill can be used for automatic horizontal filling. It must be numeric and be immediate before a string label such that a hfill label and the following string label together specifies an sprintf format such as "%*-s". The value of hfill will be set such that the resulting status bar has width equal to getOption("width")-1 (the reason for the -1 is to prevent the text status bar from writing into the next line). If more than one hfill label is used their widths will be uniformly distributed. Left over spaces will be distributed between hfill labels with initial values of one.

Author

Henrik Bengtsson

Examples

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Read all HTML files in the base package
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
path <- system.file(package="base")
files <- list.files(path, recursive=TRUE, full.names=TRUE)
files <- files[sapply(files, FUN=isFile)]
nfiles <- length(files)

cat(sprintf("Reading %d files in %s:\n", nfiles, path))
#> Reading 24 files in /home/hb/shared/software/CBI/_ubuntu24_04/R-4.6.2-gcc13/lib/R/library/base:

# Create a status bar with four labels
sb <- TextStatusBar("File: %-*s [%3.0f%% %7.0f bytes %-8s]",
                hfill=1, file="", progress=0, nbytes=0L, time="")

nbytes <- 0L
for (kk in seq_len(nfiles)) {
  file <- files[kk]

  # Update the status bar
  if (sb) {
    setLabel(sb, "progress", 100*kk/nfiles)
    if (kk %% 10 == 1 || kk == nfiles)
      setLabel(sb, "file", substr(basename(file), 1, 44))

    size <- file.info(file)$size
    # popMessage() calls update() too
    popMessage(sb, sprintf("Processing %s (%.2fkB)",
                                       basename(file), size/1024))
    flush(sb)
  }

  # Read the file
  bfr <- readBin(file, what="raw", n=size)
  nbytes <- nbytes + size

  # Emulate a slow process
  if (interactive()) Sys.sleep(rexp(1, rate=60))

  # Update the status bar
  if (sb) {
    setLabel(sb, "nbytes", nbytes)
    setLabel(sb, "time", format(Sys.time(), "%H:%M:%S"))
    update(sb)
  }
}
#> Processing CITATION (0.77kB)
#> File: CITATION                                    [  4%       0 bytes         ]File: CITATION                                    [  4%     792 bytes 17:10:35]                                                                               Processing DESCRIPTION (0.39kB)
#> File: CITATION                                    [  8%     792 bytes 17:10:35]File: CITATION                                    [  8%    1191 bytes 17:10:35]                                                                               Processing INDEX (24.25kB)
#> File: CITATION                                    [ 12%    1191 bytes 17:10:35]File: CITATION                                    [ 12%   26019 bytes 17:10:35]                                                                               Processing Rd.rds (22.10kB)
#> File: CITATION                                    [ 17%   26019 bytes 17:10:35]File: CITATION                                    [ 17%   48652 bytes 17:10:35]                                                                               Processing demo.rds (0.26kB)
#> File: CITATION                                    [ 21%   48652 bytes 17:10:35]File: CITATION                                    [ 21%   48918 bytes 17:10:35]                                                                               Processing features.rds (0.12kB)
#> File: CITATION                                    [ 25%   48918 bytes 17:10:35]File: CITATION                                    [ 25%   49041 bytes 17:10:35]                                                                               Processing hsearch.rds (22.96kB)
#> File: CITATION                                    [ 29%   49041 bytes 17:10:35]File: CITATION                                    [ 29%   72550 bytes 17:10:35]                                                                               Processing links.rds (13.30kB)
#> File: CITATION                                    [ 33%   72550 bytes 17:10:35]File: CITATION                                    [ 33%   86170 bytes 17:10:35]                                                                               Processing package.rds (0.57kB)
#> File: CITATION                                    [ 38%   86170 bytes 17:10:35]File: CITATION                                    [ 38%   86751 bytes 17:10:35]                                                                               Processing Rprofile (7.07kB)
#> File: CITATION                                    [ 42%   86751 bytes 17:10:35]File: CITATION                                    [ 42%   93990 bytes 17:10:35]                                                                               Processing base (5.28kB)
#> File: base                                        [ 46%   93990 bytes 17:10:35]File: base                                        [ 46%   99395 bytes 17:10:35]                                                                               Processing base.rdb (1087.06kB)
#> File: base                                        [ 50%   99395 bytes 17:10:35]File: base                                        [ 50% 1212545 bytes 17:10:35]                                                                               Processing base.rdx (13.85kB)
#> File: base                                        [ 54% 1212545 bytes 17:10:35]File: base                                        [ 54% 1226726 bytes 17:10:35]                                                                               Processing error.catching.R (3.29kB)
#> File: base                                        [ 58% 1226726 bytes 17:10:35]File: base                                        [ 58% 1230092 bytes 17:10:35]                                                                               Processing is.things.R (4.66kB)
#> File: base                                        [ 62% 1230092 bytes 17:10:35]File: base                                        [ 62% 1234866 bytes 17:10:35]                                                                               Processing recursion.R (2.07kB)
#> File: base                                        [ 67% 1234866 bytes 17:10:35]File: base                                        [ 67% 1236981 bytes 17:10:35]                                                                               Processing scoping.R (1.52kB)
#> File: base                                        [ 71% 1236981 bytes 17:10:35]File: base                                        [ 71% 1238541 bytes 17:10:35]                                                                               Processing AnIndex (34.97kB)
#> File: base                                        [ 75% 1238541 bytes 17:10:35]File: base                                        [ 75% 1274352 bytes 17:10:35]                                                                               Processing aliases.rds (12.45kB)
#> File: base                                        [ 79% 1274352 bytes 17:10:35]File: base                                        [ 79% 1287098 bytes 17:10:35]                                                                               Processing base.rdb (2551.35kB)
#> File: base                                        [ 83% 1287098 bytes 17:10:35]File: base                                        [ 83% 3899676 bytes 17:10:35]                                                                               Processing base.rdx (9.11kB)
#> File: base.rdx                                    [ 88% 3899676 bytes 17:10:35]File: base.rdx                                    [ 88% 3909007 bytes 17:10:35]                                                                               Processing paths.rds (3.10kB)
#> File: base.rdx                                    [ 92% 3909007 bytes 17:10:35]File: base.rdx                                    [ 92% 3912181 bytes 17:10:35]                                                                               Processing 00Index.html (148.79kB)
#> File: base.rdx                                    [ 96% 3912181 bytes 17:10:35]File: base.rdx                                    [ 96% 4064546 bytes 17:10:35]                                                                               Processing R.css (2.08kB)
#> File: R.css                                       [100% 4064546 bytes 17:10:35]File: R.css                                       [100% 4066679 bytes 17:10:35]
setLabel(sb, "file", "<done>")
update(sb)
#> File: <done>                                      [100% 4066679 bytes 17:10:35]
cat("\n")
#>