Skip to content

Commit baa859c

Browse files
shikokuchuolionel-
andauthored
Document new crating behaviour for in_parallel() (#1208)
* Document new crating behaviour * Document crating behaviour more explicitly * Update remote and add news item * Apply suggestion from @lionel- Co-authored-by: Lionel Henry <lionel.hry@gmail.com> * Add line break in news --------- Co-authored-by: Lionel Henry <lionel.hry@gmail.com>
1 parent b07e760 commit baa859c

File tree

4 files changed

+60
-25
lines changed

4 files changed

+60
-25
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Imports:
2222
rlang (>= 1.1.1),
2323
vctrs (>= 0.6.3)
2424
Suggests:
25-
carrier (>= 0.2.0),
25+
carrier (>= 0.2.0.9000),
2626
covr,
2727
dplyr (>= 0.7.8),
2828
httr,
@@ -45,3 +45,4 @@ Config/testthat/parallel: TRUE
4545
Encoding: UTF-8
4646
Roxygen: list(markdown = TRUE)
4747
RoxygenNote: 7.3.2
48+
Remotes: r-lib/carrier

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# purrr (development version)
22

3+
* `in_parallel()` now accepts objects, including helper functions, supplied to `...` for all locally-defined functions (#1208).
4+
35
* All functions that were soft-deprecated in purrr 1.0.0 are now fully deprecated. They will be removed in a future release. This includes: `invoke_*()`, `lift_*()`, `cross*()`, `prepend()`, `splice()`, `rbernoulli()`, `rdunif()`, `when()`, `update_list()`, `*_raw()`, `vec_depth()`.
6+
47
* `map_chr()` no longer coereces from logical, integer, or double to strings.
8+
59
* All functions and arguments deprecated in purrr 0.3.0 have now been removed. This includes `%@%`, `accumulate_right()`, `at_depth()`, `cross_d()`, `cross_n()`, `reduce2_right()`, and `reduce_right()`.
610

711
# purrr 1.1.0

R/parallelization.R

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@
3636
#' within the function to attach a package to the search path, which allows
3737
#' subsequent use of package functions without the explicit namespace.
3838
#'
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.
4450
#'
4551
#' [in_parallel()] is a simple wrapper of [carrier::crate()] and you may refer
4652
#' to that package for more details.
@@ -57,11 +63,13 @@
5763
#' # Use :: to namespace all package functions:
5864
#' map(1:3, in_parallel(\(x) vctrs::vec_init(integer(), x)))
5965
#'
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:
6270
#' 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))
6573
#' ```
6674
#'
6775
#' @section When to use:
@@ -135,14 +143,22 @@
135143
#' @examplesIf interactive() && rlang::is_installed("mirai") && rlang::is_installed("carrier")
136144
#' # Run in interactive sessions only as spawns additional processes
137145
#'
146+
#' delay <- function(secs = 0.5) {
147+
#' Sys.sleep(secs)
148+
#' }
149+
#'
138150
#' slow_lm <- function(formula, data) {
139-
#' Sys.sleep(0.5)
151+
#' delay()
140152
#' lm(formula, data)
141153
#' }
142154
#'
143155
#' # Example of a 'crate' returned by in_parallel(). The object print method
144156
#' # 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+
#' )
146162
#' crate
147163
#'
148164
#' # Use mirai::mirai() to test that a crate is self-contained
@@ -171,7 +187,7 @@ parallel_pkgs_installed <- function() {
171187
{
172188
check_installed(
173189
c("carrier", "mirai"),
174-
version = c("0.2.0", "2.4.0"),
190+
version = c("0.2.0.9000", "2.4.0"),
175191
reason = "for parallel map."
176192
)
177193
the$parallel_pkgs_installed <- TRUE

man/in_parallel.Rd

Lines changed: 26 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)