/
l_ply.r
41 lines (36 loc) · 1.16 KB
/
l_ply.r
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
#' Split list, apply function, and discard results.
#'
#' For each element of a list, apply function and discard results
#'
#' @template ply
#' @template l-
#' @template -_
#' @export
l_ply <- function(.data, .fun = NULL, ..., .progress = "none", .inform = FALSE,
.print = FALSE, .parallel = FALSE, .paropts = NULL) {
if (is.character(.fun) || is.list(.fun)) .fun <- each(.fun)
if (!is.function(.fun)) stop(".fun is not a function.")
progress <- create_progress_bar(.progress)
progress$init(length(.data))
on.exit(progress$term())
if (.parallel) {
if (.print) message("Printing disabled for parallel processing")
if (.progress != "none") message("Progress disabled for parallel processing")
setup_parallel()
.paropts$.combine <- function(...) NULL
fe_call <- as.call(c(list(as.name("foreach"), d = as.name(".data")),
.paropts))
fe <- eval(fe_call)
# Mute note about "no visible binding for d"
d <- NULL
fe %dopar% .fun(d, ...)
} else {
.data <- as.list(.data)
for(i in seq_along(.data)) {
x <- .fun(.data[[i]], ...)
if (.print) print(x)
progress$step()
}
}
invisible()
}