|
36 | 36 | #' within the function to attach a package to the search path, which allows |
37 | 37 | #' subsequent use of package functions without the explicit namespace. |
38 | 38 | #' |
39 | | -#' * They should declare any data they depend on. You can declare data by |
40 | | -#' supplying additional named arguments to `...`. When supplying an anonymous |
41 | | -#' function to a locally-defined function of the form `\(x) fun(x)`, the |
42 | | -#' function `fun` itself must be supplied to `...`. The entire call would then |
43 | | -#' be of the form: `in_parallel(\(x) fun(x), fun = fun)`. |
| 39 | +#' * They should declare any data they depend on. Declare data by supplying |
| 40 | +#' named arguments to `...`. When `.f` is an anonymous function to a |
| 41 | +#' locally-defined function of the form `\(x) fun(x)`, `fun` itself must be |
| 42 | +#' supplied to `...` in the manner of: `in_parallel(\(x) fun(x), fun = fun)`. |
| 43 | +#' |
| 44 | +#' * Additional arguments that are functions (closures) must themselves be |
| 45 | +#' self-contained. All objects required by them must be supplied as further |
| 46 | +#' additional arguments, if not already supplied. This applies only for |
| 47 | +#' functions directly supplied to `...`, and containers such as lists are not |
| 48 | +#' recursively walked to find functions. This means you're at risk of unexpectedly |
| 49 | +#' including large objects with your parallel function if you supply complex lists. |
44 | 50 | #' |
45 | 51 | #' [in_parallel()] is a simple wrapper of [carrier::crate()] and you may refer |
46 | 52 | #' to that package for more details. |
|
57 | 63 | #' # Use :: to namespace all package functions: |
58 | 64 | #' map(1:3, in_parallel(\(x) vctrs::vec_init(integer(), x))) |
59 | 65 | #' |
60 | | -#' fun <- function(x) { x + x %% 2 } |
61 | | -#' # Operating in parallel, locally-defined objects will not be found: |
| 66 | +#' fun <- function(x) { x + helper_fun(x) } |
| 67 | +#' helper_fun <- function(x) { x %% 2 } |
| 68 | +#' # Operating in parallel, locally-defined objects will not be found. These |
| 69 | +#' # include objects required by your functions: |
62 | 70 | #' map(1:3, in_parallel(\(x) x + fun(x))) |
63 | | -#' # Use the ... argument to supply those objects: |
64 | | -#' map(1:3, in_parallel(\(x) x + fun(x), fun = fun)) |
| 71 | +#' # Use the ... argument to supply these objects: |
| 72 | +#' map(1:3, in_parallel(\(x) x + fun(x), fun = fun, helper_fun = helper_fun)) |
65 | 73 | #' ``` |
66 | 74 | #' |
67 | 75 | #' @section When to use: |
|
135 | 143 | #' @examplesIf interactive() && rlang::is_installed("mirai") && rlang::is_installed("carrier") |
136 | 144 | #' # Run in interactive sessions only as spawns additional processes |
137 | 145 | #' |
| 146 | +#' delay <- function(secs = 0.5) { |
| 147 | +#' Sys.sleep(secs) |
| 148 | +#' } |
| 149 | +#' |
138 | 150 | #' slow_lm <- function(formula, data) { |
139 | | -#' Sys.sleep(0.5) |
| 151 | +#' delay() |
140 | 152 | #' lm(formula, data) |
141 | 153 | #' } |
142 | 154 | #' |
143 | 155 | #' # Example of a 'crate' returned by in_parallel(). The object print method |
144 | 156 | #' # shows the size of the crate and any objects contained within: |
145 | | -#' crate <- in_parallel(\(df) slow_lm(mpg ~ disp, data = df), slow_lm = slow_lm) |
| 157 | +#' crate <- in_parallel( |
| 158 | +#' \(df) slow_lm(mpg ~ disp, data = df), |
| 159 | +#' slow_lm = slow_lm, |
| 160 | +#' delay = delay |
| 161 | +#' ) |
146 | 162 | #' crate |
147 | 163 | #' |
148 | 164 | #' # Use mirai::mirai() to test that a crate is self-contained |
@@ -171,7 +187,7 @@ parallel_pkgs_installed <- function() { |
171 | 187 | { |
172 | 188 | check_installed( |
173 | 189 | c("carrier", "mirai"), |
174 | | - version = c("0.2.0", "2.4.0"), |
| 190 | + version = c("0.2.0.9000", "2.4.0"), |
175 | 191 | reason = "for parallel map." |
176 | 192 | ) |
177 | 193 | the$parallel_pkgs_installed <- TRUE |
|
0 commit comments