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:

flush-
getLabel-
newline-
popMessage-
setLabel-
setLabels-
update-
updateLabels-

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/henrik/shared/software/CBI/_ubuntu22_04/R-4.3.2-gcc11/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.63kB)
#> File: CITATION                                    [  4%       0 bytes         ]File: CITATION                                    [  4%     643 bytes 17:03:30]                                                                               Processing DESCRIPTION (0.35kB)
#> File: CITATION                                    [  8%     643 bytes 17:03:30]File: CITATION                                    [  8%     997 bytes 17:03:30]                                                                               Processing INDEX (24.07kB)
#> File: CITATION                                    [ 12%     997 bytes 17:03:30]File: CITATION                                    [ 12%   25648 bytes 17:03:30]                                                                               Processing Rd.rds (21.94kB)
#> File: CITATION                                    [ 17%   25648 bytes 17:03:30]File: CITATION                                    [ 17%   48114 bytes 17:03:30]                                                                               Processing demo.rds (0.26kB)
#> File: CITATION                                    [ 21%   48114 bytes 17:03:30]File: CITATION                                    [ 21%   48379 bytes 17:03:30]                                                                               Processing features.rds (0.12kB)
#> File: CITATION                                    [ 25%   48379 bytes 17:03:30]File: CITATION                                    [ 25%   48502 bytes 17:03:30]                                                                               Processing hsearch.rds (22.71kB)
#> File: CITATION                                    [ 29%   48502 bytes 17:03:30]File: CITATION                                    [ 29%   71754 bytes 17:03:30]                                                                               Processing links.rds (13.17kB)
#> File: CITATION                                    [ 33%   71754 bytes 17:03:30]File: CITATION                                    [ 33%   85245 bytes 17:03:30]                                                                               Processing package.rds (0.54kB)
#> File: CITATION                                    [ 38%   85245 bytes 17:03:30]File: CITATION                                    [ 38%   85793 bytes 17:03:30]                                                                               Processing Rprofile (6.88kB)
#> File: CITATION                                    [ 42%   85793 bytes 17:03:30]File: CITATION                                    [ 42%   92833 bytes 17:03:30]                                                                               Processing base (5.03kB)
#> File: base                                        [ 46%   92833 bytes 17:03:30]File: base                                        [ 46%   97985 bytes 17:03:30]                                                                               Processing base.rdb (1065.42kB)
#> File: base                                        [ 50%   97985 bytes 17:03:30]File: base                                        [ 50% 1188976 bytes 17:03:30]                                                                               Processing base.rdx (13.68kB)
#> File: base                                        [ 54% 1188976 bytes 17:03:30]File: base                                        [ 54% 1202986 bytes 17:03:30]                                                                               Processing error.catching.R (3.29kB)
#> File: base                                        [ 58% 1202986 bytes 17:03:30]File: base                                        [ 58% 1206353 bytes 17:03:30]                                                                               Processing is.things.R (4.69kB)
#> File: base                                        [ 62% 1206353 bytes 17:03:30]File: base                                        [ 62% 1211151 bytes 17:03:30]                                                                               Processing recursion.R (2.07kB)
#> File: base                                        [ 67% 1211151 bytes 17:03:30]File: base                                        [ 67% 1213266 bytes 17:03:30]                                                                               Processing scoping.R (1.52kB)
#> File: base                                        [ 71% 1213266 bytes 17:03:30]File: base                                        [ 71% 1214826 bytes 17:03:30]                                                                               Processing AnIndex (34.63kB)
#> File: base                                        [ 75% 1214826 bytes 17:03:30]File: base                                        [ 75% 1250288 bytes 17:03:30]                                                                               Processing aliases.rds (12.32kB)
#> File: base                                        [ 79% 1250288 bytes 17:03:30]File: base                                        [ 79% 1262907 bytes 17:03:30]                                                                               Processing base.rdb (2264.29kB)
#> File: base                                        [ 83% 1262907 bytes 17:03:30]File: base                                        [ 83% 3581544 bytes 17:03:30]                                                                               Processing base.rdx (9.02kB)
#> File: base.rdx                                    [ 88% 3581544 bytes 17:03:30]File: base.rdx                                    [ 88% 3590781 bytes 17:03:30]                                                                               Processing paths.rds (3.12kB)
#> File: base.rdx                                    [ 92% 3590781 bytes 17:03:30]File: base.rdx                                    [ 92% 3593975 bytes 17:03:30]                                                                               Processing 00Index.html (147.51kB)
#> File: base.rdx                                    [ 96% 3593975 bytes 17:03:30]File: base.rdx                                    [ 96% 3745027 bytes 17:03:30]                                                                               Processing R.css (1.80kB)
#> File: R.css                                       [100% 3745027 bytes 17:03:30]File: R.css                                       [100% 3746871 bytes 17:03:30]
setLabel(sb, "file", "<done>")
update(sb)
#> File: <done>                                      [100% 3746871 bytes 17:03:30]
cat("\n")
#>