Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The default behavior difference between foreach and pforeach #3

Closed
teramonagi opened this issue Jul 5, 2015 · 2 comments
Closed

The default behavior difference between foreach and pforeach #3

teramonagi opened this issue Jul 5, 2015 · 2 comments

Comments

@teramonagi
Copy link

Because the default combine function is different between foreach and pforeach, the default behaviors differ between foreach and pforeach.

> result1 <- foreach(i = 1:3) %do% {
+   list(df=iris[1:3, ], summary=summary(iris))
+ }
> class(result1[[1]])
[1] "list"
> library(pforeach)
> result2 <- pforeach(i = 1:3)({
+   list(df=iris[1:3, ], summary=summary(iris))
+ })
> class(result2[[1]])
[1] "data.frame"

To solve this issue, we have to write like the following right now in pforeach package.

> defcombine <- function(a, ...) c(a, list(...))
> result3 <- pforeach(i = 1:3, .combine=defcombine, .init=list())({
+   list(df=iris[1:3, ], summary=summary(iris))
+ })
> class(result3[[1]])
[1] "list"

Do you have any idea to deal with this more easily?

You can check the "defcombine" function used in foreach package in the following link:

@hoxo-m
Copy link
Owner

hoxo-m commented Jul 5, 2015

Set .c=list to use the default combine function of foreach.

library(foreach)
result1 <- foreach(i = 1:3) %do% {
  list(df=iris[1:3, ], summary=summary(iris))
}

library(pforeach)
result2 <- pforeach(i = 1:3, .c=list)({
  list(df=iris[1:3, ], summary=summary(iris))
})

identical(result1, result2)
[1] TRUE

@teramonagi
Copy link
Author

Thanks!
That's easy enough!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants