#' Progression
#'
#' Indique à quelle étape est rendu la boucle.
#'
#' * Si `total = 0`, affiche `0/0`.
#'
#' @param step Nombre entier allant de 1 à n.
#' @param total Nombre entier indiquant le nombre total d'itération.
#' @param desc Indiquer quel est le type de la boucle (facultatif).
#' @param lp `list(a, b)`. Fraction au format `a/b` où `a` est le nombre de loop en cours et `b` le nombre de loop totale. Voir exemple.
#'
#' @importFrom crayon blue green yellow bold red
#' @export
#' @examples
#' # 1 boucle
#' for(i in 1:5) loop_progress(step = i, total = 5,
#' desc = NULL, lp = NULL)
#' # 2 boucles
#' for(i in 1:5){
#' for(j in 1:3){
#' loop_progress(step = j, total = 3,
#' desc = "J", lp = list(2, 2))
#' }
#' loop_progress(step = i, total = 5,
#' desc = "I", lp = list(1, 2))
#' }
#' # 3 boucles
#' for(i in 1:2){
#' for(j in 1:3){
#' for(k in 1:4){
#' loop_progress(k, 4, "K", list(3,3))
#' }
#' loop_progress(j, 3, "J", list(2,3))
#' }
#' loop_progress(i, 2, "I", list(1,3))
#' }
loop_progress <- function(step, total, desc = NULL, lp = NULL){
# Fonctions ---------------------------------------------------------------------------------------
.verif_args <- function(step, total, desc, lp){
check <- newArgCheck()
if(floor(step) != step) # step doit être entier
addError("step doit être un nombre entier.", check)
if(floor(total) != total) # total doit être entier
addError("total doit être un nombre entier.", check)
if(!is.null(desc)) # desc doit être CHR
if(!is.character(desc))
addError("desc doit être une chaîne de caractères ou de type NULL.", check)
if(!is.null(lp)) # lp doit être une liste
if(!is.list(lp))
addError("lp doit être de type LIST ou NULL.", check)
finishArgCheck(check)
if(!is.null(lp)) # lp[[1]]/lp[[2]] doivent être des entiers
if(floor(lp[[1]]) != lp[[1]] | floor(lp[[2]]) != lp[[2]])
addError("lp[[1]] et lp[[2]] doivent être des nombres entiers.", check)
finishArgCheck(check)
}
# Code --------------------------------------------------------------------------------------------
.verif_args(step, total, desc, lp) # vérification des arguments
# Convertir les variables au besoin
if(!is.integer(step))
step <- as.integer(step)
if(!is.integer(total))
total <- as.integer(total)
if(!is.null(lp)){
if(!is.integer(lp[[1]]))
lp[[1]] <- as.integer(lp[[1]])
if(!is.integer(lp[[2]]))
lp[[2]] <- as.integer(lp[[2]])
}
# Indenter le message si c'est une boucle dans une autre boucle
if(!is.null(lp)){
if(lp[[1]] != 1){
if(lp[[1]] / lp[[2]] == 1){
if(step == 1){
cat(rep("\t", lp[[1]] - 1))
}
} else {
cat(rep("\t", lp[[1]] - 1))
}
}
}
# Afficher la desc de la boucle
if(!is.null(desc) && step == 1)
cat(blue(paste0("[",bold(desc),"]")), ": ")
# Message des étapes
if(total == 0){
cat(red("0/0\n"))
} else if(step == 1){
if(step / total == 1){
cat(green(paste0(step,"/",total)))
} else {
cat(yellow(paste0(step,"/",total)))
}
} else if(step > total){ # erreur si step > total
stop("step ne peut être plus grand que total.")
} else {
if(step/total == 1){
cat(";", green(paste0(step,"/",total)))
} else {
cat(";", yellow(paste0(step,"/",total)))
}
}
# Changement de ligne à la fin de la boucle
if(!is.null(lp) && lp[[1]] == 1 && step == total){ # si plusieurs loop -> \n\n
cat(".\n\n")
} else if(!is.null(lp) && lp[[1]] / lp[[2]] != 1 && step != total){
cat("\n")
}else if(step == total){ # si 1 loop -> \n
cat(".\n")
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.