本文介绍了如何在使用“foreach()”时创建一个进度条。函数在R?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 有一些关于如何在R程序中创建循环计数器的信息。但是,如何在使用foreach()的并行化版本时创建类似的函数?解析方案 编辑:在更新到doSNOW包后在使用%dopar%时它显示出一个很好的进度条,并且它可以在Linux,Windows和OS X上运行 doSNOW 现在通过 .options.snow 参数正式支持进度条。 (b)> library(doSNOW) cl registerDoSNOW(cl)迭代< -100 pb< - txtProgressBar(max =迭代,样式= 3)进度< - 函数(n)setTxtProgressBar(pb,n) opts< - list (progress = progress) result< - foreach(i = 1:iterations,.combine = rbind, .options.snow = opts)%dopar% {s< ; - 总结(rnorm(1e6))[3] return(s)} close(pb) stopCluster(cl) 跟踪进度的另一种方法是,如果记住迭代总数,则设置 .verbose = T ,因为这会打印到控制台已经完成的迭代。 以前的Linux和OS X解决方案 在Ubuntu 14.04(64位)和OS X(El Capitan)上,即使使用%dopar%如果在 makeCluster 函数中设置了 oufile =它似乎不能在Windows下工作。从 makeCluster 的帮助: $ b outfile:在哪里指定stdout和stderr连接输出工人。 表示没有重定向(这只对本地机器上的工作人员有用)。默认是'/ dev / null'(在Windows上是'nul:')。 $ b 示例代码: library(foreach) library(doSNOW) cl registerDoSNOW(c1)迭代 pb result< - foreach(i = 1:iterations,.combine = rbind)%dopar% {s setTxtProgressBar(pb,i) return $ s $ b close(pb) stopCluster(cl) 这是进度条的外观。这看起来有点奇怪,因为每一个酒吧的每一个进程打印一个新的酒吧,因为一个工人可能会滞后一点,导致进度条偶尔来回。 there are some informative posts on how to create a counter for loops in an R program. However, how do you create a similar function when using the parallelized version with "foreach()"? 解决方案 Edit: After an update to the doSNOW package it has become quite simple to display a nice progress bar when using %dopar% and it works on Linux, Windows and OS XdoSNOW now officially supports progress bars via the .options.snow argument.library(doSNOW)cl <- makeCluster(2)registerDoSNOW(cl)iterations <- 100pb <- txtProgressBar(max = iterations, style = 3)progress <- function(n) setTxtProgressBar(pb, n)opts <- list(progress = progress)result <- foreach(i = 1:iterations, .combine = rbind, .options.snow = opts) %dopar%{ s <- summary(rnorm(1e6))[3] return(s)}close(pb)stopCluster(cl) Yet another way of tracking progress, if you keep in mind the total number of iterations, is to set .verbose = T as this will print to the console which iterations have been finished.Previous solution for Linux and OS XOn Ubuntu 14.04 (64 bit) and OS X (El Capitan) the progress bar is displayed even when using %dopar% if in the makeCluster function oufile = "" is set. It does not seem to work under Windows. From the help on makeCluster:outfile: Where to direct the stdout and stderr connection output from the workers. "" indicates no redirection (which may only be useful for workers on the local machine). Defaults to ‘/dev/null’ (‘nul:’ on Windows).Example code:library(foreach)library(doSNOW)cl <- makeCluster(4, outfile="") # number of cores. Notice 'outfile'registerDoSNOW(cl)iterations <- 100pb <- txtProgressBar(min = 1, max = iterations, style = 3)result <- foreach(i = 1:iterations, .combine = rbind) %dopar% { s <- summary(rnorm(1e6))[3] setTxtProgressBar(pb, i) return(s)}close(pb)stopCluster(cl) This is what the progress bar looks like. It looks a little odd since a new bar is printed for every progression of the bar and because a worker may lag a bit which causes the progress bar to go back and forth occasionally. 这篇关于如何在使用“foreach()”时创建一个进度条。函数在R?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-20 20:28