-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.qmd
More file actions
104 lines (82 loc) · 2.15 KB
/
demo.qmd
File metadata and controls
104 lines (82 loc) · 2.15 KB
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
---
title: Try Futureverse in the Browser
format: live-html
engine: knitr
webr:
repos:
- https://cran.r-universe.dev
packages:
- future
- futurize
- future.apply
- purrr
- furrr
- foreach
- doFuture
cell-options:
echo: true
persist: true
---
You can try the future ecosystem in the below R WebAssembly ("[webR]") widget. The "only" thing is that WebAssembly does _not_ support parallel processing, so everything will run sequentially. Also, the widget buffers all output until everything has finished running.
::: {.panel-tabset}
## **futurize** 💫
_The `futurize()` function rewrites the expression to use the corresponding **future.apply**, **furrr**, and **doFuture** expression. It is part of the **[futurize]** package released in January 2026._
```{webr}
library(futurize)
plan(multisession)
slow_fcn <- function(x) {
message("x = ", x)
x^2
}
X <- 1:3
message("Evaluate it in parallel in the background")
f <- future(slow_fcn(X[1]))
y <- value(f)
print(y)
message("Futurized version of base R apply")
y <- lapply(X, slow_fcn) |> futurize()
str(y)
message("Futurized version of purrr map")
library(purrr)
y <- X |> map(slow_fcn) |> futurize()
str(y)
message("Futurized version of foreach")
library(foreach)
y <- foreach(x = X) %do% slow_fcn(x) |> futurize()
str(y)
```
## Pre-**futurize**
```{webr}
library(future)
plan(multisession)
slow_fcn <- function(x) {
message("x = ", x)
x^2
}
X <- 1:3
message("Evaluate it in parallel in the background")
f <- future(slow_fcn(X[1]))
y <- value(f)
print(y)
message("Futurized version of base R apply via future.apply")
library(future.apply)
y <- future_lapply(X, slow_fcn)
str(y)
message("Futurized version of purrr via furrr")
library(furrr)
y <- X |> future_map(slow_fcn)
str(y)
message("Futurized version of foreach via doFuture (modern)")
library(foreach)
library(doFuture)
y <- foreach(x = X) %dofuture% slow_fcn(x)
str(y)
message("Futurized version of foreach via doFuture (traditional)")
library(foreach)
doFuture::registerDoFuture()
y <- foreach(x = X) %dopar% slow_fcn(x)
str(y)
```
:::
[webR]: https://github.com/r-wasm/webr/
[futurize]: https://futurize.futureverse.org