-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogressr_future.R
112 lines (76 loc) · 2.38 KB
/
progressr_future.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Progress Bar in R
# See https://cran.r-project.org/web/packages/progressr/vignettes/progressr-intro.html
library(progressr)
n <- 500
set.seed(2021)
biomarker <- data.frame(replicate(n, sample(0:100, n/2, replace = TRUE)))
biomarker$result <- sample(1:50, size = n/2, replace = TRUE)
IVs <- names(biomarker[-length(biomarker)])
# Function as in Efficient_parallel.R
biomarker_reg <- function(IV) {
model <- as.formula(paste("result ~", IV))
summary(lm(model, data = biomarker))
}
biomarker_reg("X1")
result <- lapply(IVs, biomarker_reg)
result[[n]]
broom::glance(result[[n]])
broom::tidy(result[[n]])
#### Prepare Function for Progress Bar ####
# lapply inside function: progress using p()
biomarker_reg <- function(IVs) {
p <- progressr::progressor(along = IVs)
# p <- progressor(steps = length(IVs))
# p <- progressr::progressor(steps = length(IVs) / 10)
lapply(IVs, function(x) {
model <- as.formula(paste("result ~", x))
Sys.sleep(0.001)
p() # Here you could add a custom message
summary(lm(model, data = biomarker))
})
}
#### Proceed as normal: Don't show progress bar ####
handlers(global = FALSE) # reset in case code is re-run
rm(result)
result <- biomarker_reg(IVs)
broom::glance(result[[n]])
#### Show progress bar using with_progress() ####
handlers("txtprogressbar") # the default, in case code is re-run
with_progress(
result <- biomarker_reg(IVs)
)
#### Show progress bar using global handler ####
handlers(global = TRUE)
result <- biomarker_reg(IVs)
handlers("beepr")
# result <- biomarker_reg(IVs)
#### Use in parallel: future.apply ####
handlers(global = TRUE)
handlers("txtprogressbar")
library(future.apply)
biomarker_reg_p <- function(IVs) {
p <- progressr::progressor(along = IVs)
future_lapply(IVs, function(x) {
model <- as.formula(paste("result ~", x))
Sys.sleep(0.001)
p()
summary(lm(model, data = biomarker))
})
}
plan(sequential)
rm(result)
result <- biomarker_reg_p(IVs)
broom::glance(result[[n]])
plan(multisession)
rm(result)
result <- biomarker_reg_p(IVs)
broom::glance(result[[n]])
library(parallel)
cl <- makeCluster(detectCores() - 1)
plan(cluster)
rm(result)
result <- biomarker_reg_p(IVs)
broom::glance(result[[n]])
handlers("progress")
result <- biomarker_reg_p(IVs)
stopCluster(cl)