txtProgressBar()在R中没有显示foreach()循环

时间:2014-09-18 02:17:46

标签: r foreach parallel-processing progress-bar

需要一些包裹:

library(utils)
library(doParallel)
library(xts)

创建循环

所需的功能
CRISP = function (i){
nudata <-(crsp1[which(crsp1[,1] == paste(NAMES[1,i])),])
z=xts(coredata(nudata[,c(2)]), order.by=round(as.POSIXct(nudata[,7], format="%y-%m-%d"),  
units=c("days")))
colnames(z) <- NAMES[1,i]
return(z) 
}

注册DoParallel

cl <- makeCluster(4, type="PSOCK")
registerDoParallel(cl)
n <- dim(NAMES)[2]
pb <- txtProgressBar(min = 1, max = n, style=3)

使用foreach()循环

all_z <- foreach(i=1:dim(NAMES)[2], .combine='merge.xts', .packages='xts') %dopar% {
setTxtProgressBar(pb, i)
return(CRISP(i))}

需要可重复数据

NAMES:

NAMES <- structure(list(X1 = structure(1L, .Label = "AMERICAN CAR & FDRY CO", class = "factor"), 
X2 = structure(1L, .Label = "ALASKA JUNEAU GOLD MNG CO", class = "factor"), 
X3 = structure(1L, .Label = "AMERICAN SAFETY RAZOR CORP", class = "factor"), 
X4 = structure(1L, .Label = "AMERICAN BRAKE SHOE & FDRY", class = "factor"), 
X5 = structure(1L, .Label = "ABITIBI POWER & PAPER LTD", class = "factor")), .Names = c("X1", 
"X2", "X3", "X4", "X5"), class = "data.frame", row.names = c(NA, 
-1L))

crsp1:

crsp1 <- structure(list(COMNAM = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 2L, 
2L, 2L, 2L, 2L, 2L, 5L, 5L, 5L, 5L, 5L, 5L, 3L, 3L, 3L, 3L, 3L, 
3L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("ABITIBI POWER & PAPER LTD", 
"ALASKA JUNEAU GOLD MNG CO", "AMERICAN BRAKE SHOE & FDRY", "AMERICAN CAR & FDRY CO", 
"AMERICAN SAFETY RAZOR CORP"), class = "factor"), RET = c(45553, 
22625, 31216, 2897, 21995, 21995, 45553, 18171, 21995, 36821, 
14301, 14530, 45553, 24793, 1409, 35194, 32919, 30210, 45553, 
1, 26123, 4148, 26123, 40785, 45553, 6063, 29673, 9213, 26222, 
28048), RETX = c(45262, 22610, 31102, 2875, 21989, 21989, 45262, 
18164, 21989, 36626, 14281, 14511, 45262, 24761, 1393, 35018, 
32778, 30102, 45262, 1, 26076, 4118, 26076, 40534, 45262, 6028, 
29576, 9177, 26173, 27972), vwretd = c(NA, 0.005893, 0.001277, 
-0.003984, -0.000172, 0.007211, 0.001277, -0.003984, -0.000172, 
0.007211, -0.000804, 0.003384, NA, 0.005893, 0.001277, -0.003984, 
-0.000172, 0.007211, NA, 0.005893, 0.001277, -0.003984, -0.000172, 
0.007211, NA, 0.005893, 0.001277, -0.003984, -0.000172, 0.007211
), ewretd = c(NA, 0.009516, 0.00578, -0.001927, 0.001182, 0.008453, 
0.00578, -0.001927, 0.001182, 0.008453, -0.001689, 0.003312, 
NA, 0.009516, 0.00578, -0.001927, 0.001182, 0.008453, NA, 0.009516, 
0.00578, -0.001927, 0.001182, 0.008453, NA, 0.009516, 0.00578, 
-0.001927, 0.001182, 0.008453), sprtrn = c(NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA), DATE = structure(c(-16072, 
-16070, -16068, -16067, -16066, -16065, -16068, -16067, -16066, 
-16065, -16064, -16063, -16072, -16070, -16068, -16067, -16066, 
-16065, -16072, -16070, -16068, -16067, -16066, -16065, -16072, 
-16070, -16068, -16067, -16066, -16065), class = c("POSIXct", 
"POSIXt"), tzone = "")), .Names = c("COMNAM", "RET", "RETX", 
"vwretd", "ewretd", "sprtrn", "DATE"), row.names = c(NA, -30L
), class = "data.frame")

1 个答案:

答案 0 :(得分:4)

我简单地回答说txtProgressBar并没有被多个进程使用,所有进程都同时写入控制台,但是一个简单的测试表明它并没有太糟糕。您只需使用makePSOCKcluster outfile=""选项,以便工作人员的输出不会被丢弃:

library(doParallel)
cl <- makePSOCKcluster(3, outfile="")
registerDoParallel(cl)
pb <- txtProgressBar(min=1, max=100, style=3)
foreach(i=1:100) %dopar% {
  Sys.sleep(1)
  setTxtProgressBar(pb, i)
  i
}

请记住,结果可以由工作人员无序生成,因此当循环完成时,进度条可能不会说100%。我仍然对这样做持怀疑态度,我只是在Mac OS X上测试过这个,但是值得一试。