Feeding a function's argument into foreach's argument foreach(.combine = rbind)

foreach(.combine = 'rbind',
        .multicombine = T) {
  ...
}

I have the above function and argument. However, from the rbind() function, I would like to feed rbind(fill = T) into it. It seems from foreach documentation, I can somehow, but there is no example of it, and nothing online. Is it possible?

You can define your own function that does that and pass that instead, e.g.

rbind_with_fill <- function(...) rbind(..., fill = TRUE)

res <- foreach(.combine = rbind_with_fill,
        .multicombine = TRUE) {
  ...
}

or, by using an anonymous function, e.g.

res <- foreach(.combine = function(...) rbind(..., fill = TRUE),
        .multicombine = TRUE) {
  ...
}

PS. Use .combine = rbind instead of .combine = 'rbind'. The former passes the function, the latter the name of the function. The former is preferred and more robust against side effects.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.