pbapply: Adding Progress Bar to '*apply' Functions

apply族関数実行時にプログレスバーを表示

> library(pbapply)

バージョン: 1.3.0


関数名 概略
pbapply Adding Progress Bar to '*apply' Functions
pboptions Creating Progress Bar and Setting Options
timerProgressBar Timer Progress Bar

pbapply / pblapply / pbsapply / pbreplicate

apply族関数の実行時にプログレスバーを表示

Arguments

  • X
  • MARGIN
  • FUN
  • ...
  • simplify
  • USE.NAMES
  • n
  • expr
> x <- list(a = 1:10, beta = exp(-3:3), logic = c(TRUE,FALSE,FALSE,TRUE))
> lapply(x, mean)
$a
[1] 5.5

$beta
[1] 4.535125

$logic
[1] 0.5
> pblapply(x, mean)

  |                                                  | 0 % elapsed =00s  
  |>>>>>>>>>>>>>>>>>                                 | 33% elapsed =00s, remaining ~00s
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                | 67% elapsed =00s, remaining ~00s
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>| 100% elapsed =00s, remaining ~00s
$a
[1] 5.5

$beta
[1] 4.535125

$logic
[1] 0.5
> sapply(x, quantile)
         a        beta logic
0%    1.00  0.04978707   0.0
25%   3.25  0.25160736   0.0
50%   5.50  1.00000000   0.5
75%   7.75  5.05366896   1.0
100% 10.00 20.08553692   1.0
> pbsapply(x, mean)

  |                                                  | 0 % elapsed =00s  
  |>>>>>>>>>>>>>>>>>                                 | 33% elapsed =00s, remaining ~00s
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                | 67% elapsed =00s, remaining ~00s
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>| 100% elapsed =00s, remaining ~00s
       a     beta    logic 
5.500000 4.535125 0.500000

pboptions

> ## for loop
> fun1 <- function() {
+     pb <- startpb(0, 10)
+     on.exit(closepb(pb))
+     for (i in 1:10) {
+         Sys.sleep(0.15)
+         setpb(pb, i)
+     }
+     invisible(NULL)
+ }
> ## while loop
> fun2 <- function() {
+     pb <- startpb(0, 10-1)
+     on.exit(closepb(pb))
+     i <- 1
+     while (i < 10) {
+         Sys.sleep(0.15)
+         setpb(pb, i)
+         i <- i + 1
+     }
+     invisible(NULL)
+ }
> ## using original settings
> fun1()

  |                                                  | 0 % elapsed =00s  
  |>>>>>                                             | 10% elapsed =00s, remaining ~01s
  |>>>>>>>>>>                                        | 20% elapsed =00s, remaining ~01s
  |>>>>>>>>>>>>>>>                                   | 30% elapsed =00s, remaining ~01s
  |>>>>>>>>>>>>>>>>>>>>                              | 40% elapsed =01s, remaining ~01s
  |>>>>>>>>>>>>>>>>>>>>>>>>>                         | 50% elapsed =01s, remaining ~01s
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                    | 60% elapsed =01s, remaining ~01s
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>               | 70% elapsed =01s, remaining ~00s
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>         | 80% elapsed =01s, remaining ~00s
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>    | 90% elapsed =01s, remaining ~00s
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>| 100% elapsed =02s, remaining ~00s
> ## resetting pboptions
> opb <- pboptions(style=1, char=">")
> ## check new settings
> getOption("pboptions")
$type
[1] "timer"

$char
[1] ">"

$txt.width
[1] 50

$gui.width
[1] 300

$style
[1] 1

$initial
[1] 0

$title
[1] "R progress bar"

$label
[1] ""

$nout
[1] 100
> ## running again with new settings
> fun2()

  |                                                  | 0 % elapsed =00s  
  |>>>>>>                                            | 11% elapsed =00s, remaining ~01s
  |>>>>>>>>>>>>                                      | 22% elapsed =00s, remaining ~01s
  |>>>>>>>>>>>>>>>>>                                 | 33% elapsed =00s, remaining ~01s
  |>>>>>>>>>>>>>>>>>>>>>>>                           | 44% elapsed =01s, remaining ~01s
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>                      | 56% elapsed =01s, remaining ~01s
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                | 67% elapsed =01s, remaining ~00s
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>           | 78% elapsed =01s, remaining ~00s
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>     | 89% elapsed =01s, remaining ~00s
  |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>| 100% elapsed =01s, remaining ~00s

timerProgressBar

> testit <- function(...)
+ {
+     pb <- timerProgressBar(...)
+     for(i in seq(0, 1, 0.05)) {
+         Sys.sleep(0.2)
+         setTimerProgressBar(pb, i)
+     }
+     Sys.sleep(0.2)
+     close(pb)
+ }
> ## throbber with elapsed and remaining time
> testit(style = 2)

 / 0 % elapsed =00s  
 / 0 % elapsed =00s  
 - 5 % elapsed =00s, remaining ~08s
 \ 10% elapsed =01s, remaining ~05s
 | 15% elapsed =01s, remaining ~05s
 / 20% elapsed =01s, remaining ~04s
 - 25% elapsed =01s, remaining ~04s
 \ 30% elapsed =01s, remaining ~03s
 | 35% elapsed =02s, remaining ~03s
 / 40% elapsed =02s, remaining ~03s
 - 45% elapsed =02s, remaining ~02s
 \ 50% elapsed =02s, remaining ~02s
 | 55% elapsed =02s, remaining ~02s
 / 60% elapsed =03s, remaining ~02s
 - 65% elapsed =03s, remaining ~02s
 \ 70% elapsed =03s, remaining ~01s
 | 75% elapsed =03s, remaining ~01s
 / 80% elapsed =03s, remaining ~01s
 - 85% elapsed =04s, remaining ~01s
 \ 90% elapsed =04s, remaining ~00s
 | 95% elapsed =04s, remaining ~00s
 / 100% elapsed =04s, remaining ~00s