File: progress_aggregator.R

package info (click to toggle)
r-cran-progressr 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,132 kB
  • sloc: sh: 13; makefile: 7
file content (59 lines) | stat: -rw-r--r-- 1,357 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
source("incl/start.R")

message("progress_aggregator() ...")

with_progress({
  progress <- progressor(steps = 1 + 3 + 10 + 1)
  relay_progress <- progress_aggregator(progress)
  progress()
  relay_progress(slow_sum(1:3))
  relay_progress(slow_sum(1:10))
  progress()
})



message("- Stray progressions from unknown sources")

slow_prod <- function(x, delay = getOption("progressr.demo.delay", 0.05)) {
  progress <- progressor(2*length(x))
  res <- 0
  for (kk in seq_along(x)) {
    progress(message = sprintf("Multiplying %g", kk))
    Sys.sleep(0.8*delay)
    res <- res * x[kk]
    progress(message = "...")
    Sys.sleep(0.2*delay)
  }
  res
}

## This will only show progress for the *first* of the three
## functions that report progress. Any progression updates from
## the second and third will be ignored, because they are from
## a different source
with_progress({
  x <- 1:10
  a <- slow_sum(x)
  b <- slow_prod(x)
  c <- slow_sum(-x)
})


## To get progression from all of them, we need to know how many
## steps they report on and then use a gather-and-relay handler
with_progress({
  x <- 1:10
  progress <- progressor(3*length(x))
  relay_progress <- progress_aggregator(progress)
  relay_progress({
    a <- slow_sum(x)
    b <- slow_prod(x)
    c <- slow_sum(-x)
  })
})


message("progress_aggregator() ... done")

source("incl/end.R")