Skip to content
This repository
Browse code

Add .paropts parameter to llply. #84

  • Loading branch information...
commit eb432b6aa2d49aa7cf70f5b253f3b797c3b1469b 1 parent 05cc488
Hadley Wickham authored
7  R/parallel.r
@@ -7,3 +7,10 @@ setup_parallel <- function() {
7 7
     warning("No parallel backend registered", call. = TRUE)
8 8
   }
9 9
 }
  10
+
  11
+parallel_fe <- function(n, options) {
  12
+  i <- seq_len(n)
  13
+  fe_call <- as.call(c(list(as.name("foreach"), i = i), options))
  14
+
  15
+  eval(fe_call)
  16
+}
13  R/ply-list.r
@@ -10,6 +10,12 @@
10 10
 #' @param .inform produce informative error messages?  This is turned off by
11 11
 #'   by default because it substantially slows processing speed, but is very
12 12
 #'   useful for debugging
  13
+#' @param .paropts a list of additional options passed into
  14
+#'   the \code{\link[foreach]{foreach}} function when parallel computation
  15
+#'   is enabled.  This is important if (for example) your code relies on
  16
+#'   external data or packages: use the \code{.export} and \code{.packages}
  17
+#'   arguments to supply them so that all cluster nodes have the correct
  18
+#'   environment set up for computing.
13 19
 #' @export
14 20
 #' @examples
15 21
 #' llply(llply(mtcars, round), table)
@@ -19,7 +25,8 @@
19 25
 #'
20 26
 #' llply(x, mean)
21 27
 #' llply(x, quantile, probs = 1:3/4)
22  
-llply <- function(.data, .fun = NULL, ..., .progress = "none", .inform = FALSE, .parallel = FALSE) {
  28
+llply <- function(.data, .fun = NULL, ..., .progress = "none", .inform = FALSE,
  29
+                  .parallel = FALSE, .paropts = NULL) {
23 30
   if (is.null(.fun)) return(as.list(.data))
24 31
   if (is.character(.fun) || is.list(.fun)) .fun <- each(.fun)
25 32
   if (!is.function(.fun)) stop(".fun is not a function.")
@@ -68,7 +75,9 @@ llply <- function(.data, .fun = NULL, ..., .progress = "none", .inform = FALSE,
68 75
   }
69 76
   if (.parallel) {
70 77
     setup_parallel()
71  
-    result <- foreach(i = seq_len(n)) %dopar% do.ply(i)
  78
+    fe <- parallel_fe(n, .paropts)
  79
+
  80
+    result <- fe %dopar% do.ply(i)
72 81
   } else {
73 82
     result <- loop_apply(n, do.ply)
74 83
   }
9  inst/tests/test-parallel.r
@@ -17,3 +17,12 @@ test_that("l_ply + .parallel complains about invalid arguments", {
17 17
     l_ply(1:10, force, .parallel = TRUE, .progress = "text"),
18 18
     "Progress disabled")
19 19
 })
  20
+
  21
+test_that(".paropts passes options to foreach", {
  22
+  combine <- function(a, b) NULL
  23
+  x <- llply(1:10, identity, .parallel = TRUE,
  24
+    .paropts = list(.combine = combine))
  25
+  expect_equal(x, NULL)
  26
+})
  27
+
  28
+registerDoMC(1)
10  man/llply.Rd
@@ -3,7 +3,7 @@
3 3
 \title{Split list, apply function, and return results in a list.}
4 4
 \usage{
5 5
   llply(.data, .fun = NULL, ..., .progress = "none",
6  
-    .inform = FALSE, .parallel = FALSE)
  6
+    .inform = FALSE, .parallel = FALSE, .paropts = NULL)
7 7
 }
8 8
 \arguments{
9 9
   \item{.fun}{function to apply to each piece}
@@ -21,6 +21,14 @@
21 21
   \item{.inform}{produce informative error messages?  This
22 22
   is turned off by by default because it substantially
23 23
   slows processing speed, but is very useful for debugging}
  24
+
  25
+  \item{.paropts}{a list of additional options passed into
  26
+  the \code{\link[foreach]{foreach}} function when parallel
  27
+  computation is enabled.  This is important if (for
  28
+  example) your code relies on external data or packages:
  29
+  use the \code{.export} and \code{.packages} arguments to
  30
+  supply them so that all cluster nodes have the correct
  31
+  environment set up for computing.}
24 32
 }
25 33
 \value{
26 34
   list of results

0 notes on commit eb432b6

Please sign in to comment.
Something went wrong with that request. Please try again.