TextStatusBar            package:R.utils            R Documentation

_A _s_t_a_t_u_s _b_a_r _a_t _t_h_e _R _p_r_o_m_p_t _t_h_a_t _c_a_n _b_e _u_p_d_a_t_e_d

_D_e_s_c_r_i_p_t_i_o_n:

     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.

_U_s_a_g_e:

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

_A_r_g_u_m_e_n_t_s:

     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.

_D_e_t_a_i_l_s:

     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.

_F_i_e_l_d_s _a_n_d _M_e_t_h_o_d_s:

     *Methods:*

         'flush'         Flushes the output.
         'getLabel'      Gets the current value of a label.
         'newline'       Writes a newline.
         'popMessage'    Adds a message above the status bar.
         'setLabel'      Sets the value of a label.
         'setLabels'     Sets new values of given labels.
         'update'        Updates the status bar (visually).
         'updateLabels'  Sets the new values of given labels and updates the status bar.

     *Methods inherited from Object*:
      $, $<-, [[, [[<-, as.character, attach, attachLocally,
     clearCache, clone, detach, equals, extend, finalize, gc,
     getEnvironment, getFields, getInstanciationTime,
     getStaticInstance, hasField, hashCode, ll, load, objectSize,
     print, save

_A_u_t_h_o_r(_s):

     Henrik Bengtsson (<URL: http://www.braju.com/R/>)

_E_x_a_m_p_l_e_s:

     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     # Read all HTML files in the base package
     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     path <- system.file("html", package="base");
     files <- list.files(path, full.names=TRUE)
     nfiles <- length(files)

     cat(sprintf("Reading %d files in %s:\n", nfiles, path))

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

     nlines <- 0
     for (kk in seq(length=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/1024;
         # popMessage() calls update() too
         popMessage(sb, sprintf("Processing %s (%.2fkB)",
                                            basename(file), size))
         flush(sb)
       }

       # Read the file
       lines <- readLines(file)
       nlines <- nlines + length(lines)

       # Emulate slow process
       if (interactive()) {
         Sys.sleep(rexp(1, rate=40))
       }

       # Update the status bar
       if (sb) {
         setLabel(sb, "nlines", nlines)
         setLabel(sb, "time", format(Sys.time(), "%H:%M:%S"))
         update(sb)
       }
     }
     cat("\n")

