Skip to content

Commit 818dff2

Browse files
README: add section on common sequential iterator packages and functions
1 parent 93bdc4a commit 818dff2

File tree

2 files changed

+130
-6
lines changed

2 files changed

+130
-6
lines changed

OVERVIEW.md

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,51 @@ handlers("txtprogressbar", "beepr")
105105

106106
## Support for progressr elsewhere
107107

108-
### The future framework
108+
Note that progression updates by **progressr** is designed to work out of the box for any _sequential_ iterator framework in R. Here is an set of examples for the most common ones:
109+
110+
```r
111+
library(progressr)
112+
113+
xs <- 1:5
114+
y_truth <- lapply(xs, slow_sqrt)
115+
116+
with_progress({
117+
p <- progressor(along = xs)
118+
y <- lapply(xs, function(x) {
119+
p(sprintf("x=%g", x))
120+
Sys.sleep(0.1)
121+
sqrt(x)
122+
})
123+
})
124+
125+
library(foreach)
126+
with_progress({
127+
p <- progressor(along = xs)
128+
y <- foreach(x = xs) %do% {
129+
p(sprintf("x=%g", x))
130+
Sys.sleep(0.1)
131+
sqrt(x)
132+
}
133+
})
134+
135+
library(purrr)
136+
with_progress({
137+
p <- progressor(along = xs)
138+
y <- map(xs, function(x) {
139+
p(sprintf("x=%g", x))
140+
Sys.sleep(0.1)
141+
sqrt(x)
142+
})
143+
})
144+
```
109145

110-
The **[future]** framework has built-in support for the kind of progression updates produced by the **progressr** package. This means that you can use it with for instance **[future.apply]**, **[furrr]**, and **[foreach]** with **[doFuture]**.
111146

147+
### Parallel processing and progress updates
112148

113-
#### future_lapply()
149+
The **[future]** framework, which provides a unified API for parallel and distributed processing in R, has built-in support for the kind of progression updates produced by the **progressr** package. This means that you can use it with for instance **[future.apply]**, **[furrr]**, and **[foreach]** with **[doFuture]**.
150+
151+
152+
#### future_lapply() - parallel lapply()
114153

115154
Here is an example that uses `future_lapply()` of the **[future.apply]** package to parallelize on the local machine while at the same time signaling progression updates:
116155

@@ -161,6 +200,29 @@ with_progress({
161200
```
162201

163202

203+
#### future_map() - parallel purrr::map()
204+
205+
```r
206+
library(furrr)
207+
plan(multisession)
208+
209+
library(progressr)
210+
handlers("progress", "beepr")
211+
212+
xs <- 1:5
213+
214+
with_progress({
215+
p <- progressor(along = xs)
216+
y <- future_map(xs, function(x) {
217+
p(sprintf("x=%g", x))
218+
Sys.sleep(6.0-x)
219+
sqrt(x)
220+
})
221+
})
222+
## [=================>-----------------------------] 40% x=2
223+
```
224+
225+
164226
### The plyr package
165227

166228
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,

README.md

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,51 @@ handlers("txtprogressbar", "beepr")
107107

108108
## Support for progressr elsewhere
109109

110-
### The future framework
110+
Note that progression updates by **progressr** is designed to work out of the box for any _sequential_ iterator framework in R. Here is an set of examples for the most common ones:
111+
112+
```r
113+
library(progressr)
114+
115+
xs <- 1:5
116+
y_truth <- lapply(xs, slow_sqrt)
117+
118+
with_progress({
119+
p <- progressor(along = xs)
120+
y <- lapply(xs, function(x) {
121+
p(sprintf("x=%g", x))
122+
Sys.sleep(0.1)
123+
sqrt(x)
124+
})
125+
})
126+
127+
library(foreach)
128+
with_progress({
129+
p <- progressor(along = xs)
130+
y <- foreach(x = xs) %do% {
131+
p(sprintf("x=%g", x))
132+
Sys.sleep(0.1)
133+
sqrt(x)
134+
}
135+
})
136+
137+
library(purrr)
138+
with_progress({
139+
p <- progressor(along = xs)
140+
y <- map(xs, function(x) {
141+
p(sprintf("x=%g", x))
142+
Sys.sleep(0.1)
143+
sqrt(x)
144+
})
145+
})
146+
```
111147

112-
The **[future]** framework has built-in support for the kind of progression updates produced by the **progressr** package. This means that you can use it with for instance **[future.apply]**, **[furrr]**, and **[foreach]** with **[doFuture]**.
113148

149+
### Parallel processing and progress updates
114150

115-
#### future_lapply()
151+
The **[future]** framework, which provides a unified API for parallel and distributed processing in R, has built-in support for the kind of progression updates produced by the **progressr** package. This means that you can use it with for instance **[future.apply]**, **[furrr]**, and **[foreach]** with **[doFuture]**.
152+
153+
154+
#### future_lapply() - parallel lapply()
116155

117156
Here is an example that uses `future_lapply()` of the **[future.apply]** package to parallelize on the local machine while at the same time signaling progression updates:
118157

@@ -163,6 +202,29 @@ with_progress({
163202
```
164203

165204

205+
#### future_map() - parallel purrr::map()
206+
207+
```r
208+
library(furrr)
209+
plan(multisession)
210+
211+
library(progressr)
212+
handlers("progress", "beepr")
213+
214+
xs <- 1:5
215+
216+
with_progress({
217+
p <- progressor(along = xs)
218+
y <- future_map(xs, function(x) {
219+
p(sprintf("x=%g", x))
220+
Sys.sleep(6.0-x)
221+
sqrt(x)
222+
})
223+
})
224+
## [=================>-----------------------------] 40% x=2
225+
```
226+
227+
166228
### The plyr package
167229

168230
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,

0 commit comments

Comments
 (0)