Next with Revolution R's foreach package?

前端 未结 2 832
灰色年华
灰色年华 2021-02-07 04:09

I\'ve looked through much of the documentation and done a fair amount of Googling, but can\'t find an answer to the following question: Is there a way to induce \'next-like\' fu

相关标签:
2条回答
  • 2021-02-07 04:17

    Although it seems strange, you can use a return in the body of a foreach loop, without the need for an auxiliary function (as demonstrated by @Aaron):

    r <- foreach(i = 1:10, .combine='c') %dopar% {
      n <- i + floor(runif(1, 0, 9))
      if (n %% 3) return(NULL)
      n
    }
    

    A NULL is returned in this example since it is filtered out by the c function, which can be useful.

    Also, although it doesn't work well for your example, the when function can take the place of next at times, and is useful for preventing the computation from taking place at all:

    r <- foreach(i=1:5, .combine='c') %:%
             foreach(j=1:5, .combine='c') %:%
                 when (i != j) %dopar% {
                     10 * i + j
                 }
    

    The inner expression is only evaluated 20 times, not 25. This is particularly useful with nested foreach loops, since when has access to all of the upstream iterator values.


    Update

    If you want to filter out NULLs when returning the results in a list, you need to write your own combine function. Here's a complete example that demonstrates a combine function that works like the default combine function but includes a filtering mechanism:

    library(doSNOW)
    cl <- makeSOCKcluster(3)
    registerDoSNOW(cl)
    
    filteredlist <- function(a, ...) {
      values <- list(...)
      c(a, values[! sapply(values, is.null)])
    }
    
    r <- foreach(i=1:200, .combine='filteredlist', .init=list(),
                 .multicombine=TRUE) %dopar% {
      # filter out odd values of i
      if (i %% 2) return(NULL)
      i
    }
    

    Note that this code works correctly when there are more than 100 task results (100 is the default value of the .maxcombine option).

    0 讨论(0)
  • 2021-02-07 04:32

    You could put your code in a function and call return. It's not clear from your example what you want it to do when n %% 3 so I'll return NA.

    funi <- function(i) {
      n <- i + floor(runif(1, 0, 9))
      if (n %% 3) return(NA)
      n
    }
    foreach(i = 1:10, .combine = "c") %dopar% { funi(i) }
    
    0 讨论(0)
提交回复
热议问题