hacker

Tricks and Convenience functions for GNU R
Log | Files | Refs

statusbar.R (1053B)


      1 #  Copyright (C) 2019 Jan "JayVii"
      2 #  Author 2019 Jan "JayVii" <jayvii@posteo.de>
      3 #  SPDX-License-Identifier: GPL-3.0
      4 #  About this header: <https://reuse.software>
      5 
      6 # statusbar for loops and other index-driven iterations -----------------------
      7 
      8 statusbar <- function(i, I, c80 = TRUE, perc = TRUE, stepsize = 1, pch = "."){
      9 
     10     # set modulo according to whether percentages shall be shown or not
     11     if (perc) mod <- 75 else mod <- 80
     12 
     13     # calculate modulo for column-80-rule
     14     if (c80) m80 <- i %% (mod * stepsize) else m80 <- -1
     15 
     16     # if new line starts, print percentage
     17     if (m80 == 1){
     18         ## calculate current progress in percent
     19         ip <- round((i / I) * 100)
     20         ## fill up chars until status is 3 characters long
     21         while (nchar(ip) < 3) ip <- paste0(" ", ip)
     22         ## finally, print percentage
     23         cat(paste0(ip, "% "))
     24     }
     25 
     26     # for every step, print a dot
     27     if (i %% stepsize == 0) cat(pch)
     28 
     29     # if a column is full, do a line-break (column-80-rule)
     30     if (m80 == 0) cat("\n")
     31 
     32 }
     33 
     34 # EOF statusbar.R
     35