Skip to content

Commit feac10b

Browse files
authored
Add 'options' argument for collect_mirai() (#191)
* remove NSE from collect_mirai() * rationalise cli init function * docs: mention helper functions
1 parent 9d0aeb8 commit feac10b

File tree

7 files changed

+33
-22
lines changed

7 files changed

+33
-22
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
+ The cli package is used, if installed, for richer progress bars and error messages.
77
+ `[.progress_cli]` is no longer a separate option.
88
+ `[.stop]` now reports the index number that errored.
9+
* `collect_mirai()` replaces '...' with an 'options' argument, to which collection options should be supplied as a character vector. This avoids non-standard evaluation in this function.
910
* `daemon()` now returns an integer exit code to indicate the reason for termination.
1011
* Adds `nextcode()` to provide a human-readable translation of the exit codes returned by `daemon()`.
1112
* `everywhere()` now returns a list of at least one mirai regardless of the number of actual connections.

R/map.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,10 @@ mirai_map <- function(.x, .f, ..., .args = list(), .promise = NULL, .compute = "
212212
`[.mirai_map` <- function(x, ...) {
213213

214214
missing(..1) && return(collect_aio_(x))
215-
map(x, ...)
215+
216+
ensure_cli_initialized()
217+
dots <- eval(`[[<-`(substitute(alist(...)), 1L, quote(list)), envir = .)
218+
map(x, dots)
216219

217220
}
218221

@@ -266,14 +269,14 @@ print.mirai_map <- function(x, ...) {
266269

267270
# internals --------------------------------------------------------------------
268271

269-
map <- function(x, ...) {
270-
272+
ensure_cli_initialized <- function()
271273
if (is.null(.[[".flat"]])) {
272274
cli <- requireNamespace("cli", quietly = TRUE)
273-
`[[<-`(`[[<-`(`[[<-`(., ".flat", if (cli) flat_cli else .flat),".progress", if (cli) progress_cli else .progress), ".stop", if (cli) stop_cli else .stop)
275+
`[[<-`(`[[<-`(`[[<-`(., ".flat", if (cli) flat_cli else .flat), ".progress", if (cli) progress_cli else .progress), ".stop", if (cli) stop_cli else .stop)
274276
}
275277

276-
dots <- eval(`[[<-`(substitute(alist(...)), 1L, quote(list)), envir = .)
278+
map <- function(x, dots) {
279+
277280
expr <- if (length(dots) > 1L) do.call(expression, dots) else dots[[1L]]
278281
xlen <- length(x)
279282
i <- 0L

R/mirai.R

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@
7474
#' part of \sQuote{.expr}.
7575
#'
7676
#' For evaluation to occur \emph{as if} in your global environment, supply
77-
#' objects to \sQuote{...} rather than \sQuote{.args}. This is important when
78-
#' supplying free variables defined in function bodies, as scoping rules may
77+
#' objects to \sQuote{...} rather than \sQuote{.args}, e.g. for free variables
78+
#' or helper functions defined in function bodies, as scoping rules may
7979
#' otherwise prevent them from being found.
8080
#'
8181
#' @section Timeouts:
@@ -352,8 +352,10 @@ call_mirai_ <- call_aio_
352352
#' \code{x}, and is equivalent to \code{collect_mirai(x)}.
353353
#'
354354
#' @inheritParams call_mirai
355-
#' @param ... (if \sQuote{x} is a list of mirai) any of the collection options
356-
#' for \code{\link{mirai_map}}, such as \code{.flat}.
355+
#' @param options (if \sQuote{x} is a list of mirai) a character vector
356+
#' comprising any combination of collection options for
357+
#' \code{\link{mirai_map}}, such as \code{".flat"} or
358+
#' \code{c(".progress", ".stop")}.
357359
#'
358360
#' @return An object (the return value of the \sQuote{mirai}), or a list of such
359361
#' objects (the same length as \sQuote{x}, preserving names).
@@ -377,17 +379,20 @@ call_mirai_ <- call_aio_
377379
#' # mirai_map with collection options
378380
#' daemons(1, dispatcher = FALSE)
379381
#' m <- mirai_map(1:3, rnorm)
380-
#' collect_mirai(m, .flat, .progress)
382+
#' collect_mirai(m, c(".flat", ".progress"))
381383
#' daemons(0)
382384
#'
383385
#' }
384386
#'
385387
#' @export
386388
#'
387-
collect_mirai <- function(x, ...) {
389+
collect_mirai <- function(x, options = NULL) {
388390

389-
is.list(x) && ...length() && return(map(x, ...))
390-
collect_aio_(x)
391+
is.list(x) && length(options) || return(collect_aio_(x))
392+
393+
ensure_cli_initialized()
394+
dots <- mget(options, envir = .)
395+
map(x, dots)
391396

392397
}
393398

man/collect_mirai.Rd

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

man/everywhere.Rd

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

man/mirai.Rd

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

tests/tests.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ connection && Sys.getenv("NOT_CRAN") == "true" && {
249249
q <- quote({ list2env(list(b = 2), envir = .GlobalEnv); 0L})
250250
mm <- everywhere(q)
251251
test_type("list", mm)
252-
test_zero(collect_mirai(mm, .flat))
252+
test_zero(collect_mirai(mm, ".flat"))
253253
m <- mirai(b, .timeout = 1000)
254254
if (!is_error_value(m[])) test_equal(m[], 2L)
255255
test_null(saisei(1))

0 commit comments

Comments
 (0)