Skip to content

Commit

Permalink
add parallelized examples to map(); document with() method for daemons
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jan 22, 2025
1 parent 8273941 commit 44298f4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
13 changes: 13 additions & 0 deletions R/map.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@
#' map(\(df) lm(mpg ~ wt, data = df)) |>
#' map(summary) |>
#' map_dbl("r.squared")
#'
#' # To use parallelized map, set daemons (number of parallel processes) first:
#' daemons(1, dispatcher = FALSE) # set to > 1 in real usage
#' mtcars |> map_dbl(sum, .parallel = TRUE)
#' daemons(0)
#'
#' # Or wrap a statement in with() to evaluate it with the specified daemons:
#' with(daemons(1, dispatcher = FALSE), {
#' 1:10 |>
#' map(rnorm, n = 10, .parallel = TRUE) |>
#' map_dbl(mean, .parallel = TRUE)
#' })
#'
map <- function(.x, .f, ..., .parallel = FALSE, .progress = FALSE) {
map_("list", .x, .f, ..., .parallel = .parallel, .progress = .progress)
}
Expand Down
14 changes: 14 additions & 0 deletions R/parallelization.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@
#' specifically terminate daemons in this instance, although it is still good
#' practice to do so.
#'
#' ## with() method
#'
#' A statement may be evaluated with daemons set up for the duration required.
#' The `with()` method for daemons provides a convenient way of doing this. As
#' an example:
#'
#' \preformatted{
#' with(daemons(7), {
#' 1:10 |>
#' map(rnorm, n = 10, .parallel = TRUE) |>
#' map_dbl(mean, .parallel = TRUE)
#' })
#' }
#'
#' ## Further documentation
#'
#' \pkg{purrr}'s parallelization is powered by \CRANpkg{mirai}, so see the
Expand Down
13 changes: 13 additions & 0 deletions man/map.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions man/parallelization.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 44298f4

Please sign in to comment.