Learn R Programming

rje (version 1.12.1)

printPercentage: Print Percentage of Activity Completed to stdout

Description

Prints percentage (or alternatively just a count) of loop or similar process which has been completed to the standard output.

Usage

printPercentage(i, n, dp = 0, first = 1, last = n, prev = i - 1)

Value

NULL

Arguments

i

the number of iterations completed.

n

total number of iterations.

dp

number of decimal places to display.

first

number of the first iteration for which this percentage was displayed

last

number of the final iteration for which this percentage will be displayed

prev

number of the previous iteration for which this percentage was displayed

Warning

This will fail to work nicely if other information is printed to the standard output

Author

Robin Evans

Details

printPercentage will use cat to print the proportion of loops which have been completed (i.e. i/n) to the standard output. In doing so it will erase the previous such percentage, except when i = first. A new line is added when i = last, assuming that the loop is finished.

Examples

Run this code

x = numeric(100)

for (i in 1:100) {
  x[i] = mean(rnorm(1e5))
  printPercentage(i,100)
}


i = 0
repeat {
  i = i+1
  if (runif(1) > 0.99) {
    break
  }
  printCount(i)
}
print("\n")

Run the code above in your browser using DataLab