forked from greta-dev/greta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallbacks.R
72 lines (62 loc) · 1.61 KB
/
callbacks.R
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
60
61
62
63
64
65
66
67
68
69
70
71
72
# functions to be run whilst samplers are running in parallel
#' @importFrom utils read.table
read_trace_log_file <- function(filename) {
ans <- NULL
if (file.exists(filename)) {
ans <- read.table(filename)
}
ans
}
read_progress_log_file <- function(filename, skip = 0) {
ans <- ""
if (file.exists(filename)) {
ans <- suppressWarnings(readLines(filename))
idx <- (skip + 1):length(ans)
ans <- ans[idx]
}
ans
}
# generic rendering of progress text straight from a set of files
#' @importFrom utils flush.console
render_progress <- function(reads) {
reads <- unlist(reads)
reads[is.na(reads)] <- ""
some_results <- any(nchar(reads) > 0)
if (some_results) {
# optionally add blanks to put lines at the edges
if (length(reads) > 1) {
reads <- c("", reads, "")
}
msg <- paste(reads, collapse = " | ")
message("\r", appendLF = FALSE)
message(msg, appendLF = FALSE)
flush.console()
}
}
# callback functions
percentages <- function() {
reads <- lapply(
greta_stash$percentage_log_files,
read_progress_log_file
)
render_progress(reads)
}
progress_bars <- function() {
reads <- lapply(
greta_stash$progress_bar_log_files,
read_progress_log_file,
skip = 1
)
render_progress(reads)
}
# determine the type of progress information
set_progress_bar_type <- function(n_chain) {
if (bar_width(n_chain) < 42) {
progress_callback <- percentages
} else {
progress_callback <- progress_bars
}
greta_stash$callbacks$parallel_progress <- progress_callback
}
# register some
greta_stash$callbacks <- list(parallel_progress = progress_bars)