r foreach:修改.combine创建相当于dplyr :: bind_rows并包括进度栏

发布于 2025-01-31 18:38:47 字数 874 浏览 3 评论 0 原文

采用以下MWE:

df <- data.frame(id =seq(1,100,1),random = seq(2,200,2))

cl <- parallel::makeForkCluster(5) # do not change
doParallel::registerDoParallel(cl) # do not change


results <- foreach::foreach(i=unique(df$id),.combine =dplyr::bind_rows) %dopar% {
data <- df %>% dplyr::filter(id == i) %>% 
dplyr::mutate(result = random^2) # just a non-sense calculation
}

parallel::stopCluster(cl)

由于几个原因,我想坚持 makeForkCluster 启动并行后端,我也想获取一个data.frame而不是列表。

有两个问题:

  1. 如何创建
comb <- function() 

与上述完全相同的结果,并使用 .combine ='comb'。我真的很挣扎着 .combine

  1. 有什么方法可以在该MWE中包含一个简单的进度栏 - 我知道还有其他几个包/后端可以用于Inlcude Progress Bars,但是我想使用 MakeForkCluster 从软件包 >并行。

Take the following MWE:

df <- data.frame(id =seq(1,100,1),random = seq(2,200,2))

cl <- parallel::makeForkCluster(5) # do not change
doParallel::registerDoParallel(cl) # do not change


results <- foreach::foreach(i=unique(df$id),.combine =dplyr::bind_rows) %dopar% {
data <- df %>% dplyr::filter(id == i) %>% 
dplyr::mutate(result = random^2) # just a non-sense calculation
}

parallel::stopCluster(cl)

For several reasons I want to stick to makeForkCluster to start a parallel backend and I also want to get a data.frame and not a list.

There are two questions:

  1. How do I have to create
comb <- function() 

to produce exactly the same results as above with .combine = 'comb'. I'm really struggling with .combine.

  1. Is there any way to include a simple progress bar in that MWE - I know there are several other packages/backends to inlcude progress bars, but I want to stick to the MWE above using makeForkCluster from package parallel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文