1

Some packages needed:

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

Create the function needed for loop

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) } 

Register DoParallel

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

Use foreach() to loop

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

REPRODUCIBLE DATA NEEDED

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") 
3
  • When I ran the provided code I got Error in { : task 1 failed - "incorrect number of dimensions" Commented Sep 18, 2014 at 3:38
  • @josilber thanks for responding. There was a problem with the crsp1 code. I have updated it just now. Commented Sep 18, 2014 at 4:56
  • I suspect what is happening is that the stdout for each child process isn't being redirected to the parent process. Commented Sep 18, 2014 at 5:59

1 Answer 1

4

I was going to simply answer that txtProgressBar wasn't designed to be used by multiple processes, all writing to the console at the same time, but a simple test showed that it doesn't work too badly. You just have to use the makePSOCKcluster outfile="" option so that output from the workers isn't thrown away:

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 } 

Keep in mind that the results can be produced out-of-order by the workers, so the progress bar may not say 100% when the loop completes. I'm still skeptical about doing this, and I only tested this on Mac OS X, but it's worth trying.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.