Skip to content

Commit 2758c60

Browse files
Make use of progressor(along = x)
1 parent 89e5374 commit 2758c60

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

OVERVIEW.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Assume that we have a function `slow_sum()` for adding up the values in a vector
2626

2727
```r
2828
slow_sum <- function(x) {
29-
progress <- progressr::progressor(length(x))
29+
progress <- progressr::progressor(along = x)
3030
sum <- 0
3131
for (kk in seq_along(x)) {
3232
Sys.sleep(0.1)
@@ -109,16 +109,22 @@ handlers("txtprogressbar", "beepr")
109109

110110
The functions in the [**plyr**](https://cran.r-project.org/package=plyr) package take argument `.progress`, which can be used to produce progress updates. To have them generate **progressr** 'progression' updates, use `.progress = "progressr"`. For example,
111111
```r
112+
library(plyr)
112113
library(progressr)
114+
115+
xs <- 1:5
116+
113117
with_progress({
114-
y <- plyr::l_ply(1:5, function(x, ...) {
118+
y <- l_ply(xs, function(x, ...) {
115119
Sys.sleep(6.0-x)
116120
sqrt(x)
117121
}, .progress = "progressr")
118122
})
119123
## |===================== | 40%
120124
```
121125

126+
_Comment_: This also works when using `.parallel = TRUE` with a **[foreach]** parallel-backend such as **[doParallel]** or **[doFuture]** registered.
127+
122128

123129
### The future framework
124130

@@ -136,9 +142,11 @@ plan(multisession)
136142
library(progressr)
137143
handlers("progress", "beepr")
138144

145+
xs <- 1:5
146+
139147
with_progress({
140-
p <- progressr::progressor(5)
141-
y <- future_lapply(1:5, function(x, ...) {
148+
p <- progressor(along = xs)
149+
y <- future_lapply(xs, function(x, ...) {
142150
p(sprintf("x=%g", x))
143151
Sys.sleep(6.0-x)
144152
sqrt(x)
@@ -160,8 +168,10 @@ plan(multisession)
160168
library(progressr)
161169
handlers("progress", "beepr")
162170

171+
xs <- 1:5
172+
163173
with_progress({
164-
p <- progressr::progressor(5)
174+
p <- progressor(5)
165175
y <- foreach(x = 1:5) %dopar% {
166176
p(sprintf("x=%g", x))
167177
Sys.sleep(6.0-x)

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Assume that we have a function `slow_sum()` for adding up the values in a vector
2828

2929
```r
3030
slow_sum <- function(x) {
31-
progress <- progressr::progressor(length(x))
31+
progress <- progressr::progressor(along = x)
3232
sum <- 0
3333
for (kk in seq_along(x)) {
3434
Sys.sleep(0.1)
@@ -111,16 +111,22 @@ handlers("txtprogressbar", "beepr")
111111

112112
The functions in the [**plyr**](https://cran.r-project.org/package=plyr) package take argument `.progress`, which can be used to produce progress updates. To have them generate **progressr** 'progression' updates, use `.progress = "progressr"`. For example,
113113
```r
114+
library(plyr)
114115
library(progressr)
116+
117+
xs <- 1:5
118+
115119
with_progress({
116-
y <- plyr::l_ply(1:5, function(x, ...) {
120+
y <- l_ply(xs, function(x, ...) {
117121
Sys.sleep(6.0-x)
118122
sqrt(x)
119123
}, .progress = "progressr")
120124
})
121125
## |===================== | 40%
122126
```
123127

128+
_Comment_: This also works when using `.parallel = TRUE` with a **[foreach]** parallel-backend such as **[doParallel]** or **[doFuture]** registered.
129+
124130

125131
### The future framework
126132

@@ -138,9 +144,11 @@ plan(multisession)
138144
library(progressr)
139145
handlers("progress", "beepr")
140146

147+
xs <- 1:5
148+
141149
with_progress({
142-
p <- progressr::progressor(5)
143-
y <- future_lapply(1:5, function(x, ...) {
150+
p <- progressor(along = xs)
151+
y <- future_lapply(xs, function(x, ...) {
144152
p(sprintf("x=%g", x))
145153
Sys.sleep(6.0-x)
146154
sqrt(x)
@@ -162,8 +170,10 @@ plan(multisession)
162170
library(progressr)
163171
handlers("progress", "beepr")
164172

173+
xs <- 1:5
174+
165175
with_progress({
166-
p <- progressr::progressor(5)
176+
p <- progressor(5)
167177
y <- foreach(x = 1:5) %dopar% {
168178
p(sprintf("x=%g", x))
169179
Sys.sleep(6.0-x)

0 commit comments

Comments
 (0)