how to save the output of a foreach loop in R

妖精的绣舞 提交于 2019-12-13 15:19:30

问题


I have a trouble with saving my data output after foreach loop

here is the function to read my data and process it

readFiles <- function(x){
   data   <- read.table("filelist",
      skip=grep('# Begin: Data Text', readLines(filelist)),
      na.strings=c("NA", "-", "?"),
      colClasses="numeric")
   my     <- as.matrix(data[1:57600,2]);
   mesh   <- array(my, dim = c(120,60,8));
   Ms     <- 1350*10^3    # A/m
   asd2   <- (mesh[70:75,24:36 ,2])/Ms;     # in A/m
   ort_my <- mean(asd2);
   return(ort_my)
}

here is the codes for doing parallel process

#R Code to run functions in parallel 
detectCores() #This will tell you how many cores are available 
library("foreach");
library("parallel");
library(doParallel)
#library("doMC") this is for Linux 
#registerDoMC(12) #Register the parallel backend
cl<-makeCluster(4)
registerDoParallel(cl)   # Register 12 cpu for the parallel backend
OutputList <- foreach(i=1:length(filelist),
   .combine='c', .packages=c("data.table")) %dopar% (readFiles) 
#registerDoSEQ() #Very important to close out parallel backend.
aa<-OutputList
stopCluster(cl)
print(Sys.time()-strt)
write.table(aa, file="D:/ads.txt",sep='\t')

Everything goes smoothly but when I check OutputList what I see only function(x) I want to write ort_my for each file in filelist.

here is what I see

[[70]]
function (x) 
{
data <- read.table("filelist", skip = grep("# Begin: Data Text", 
    readLines(filelist)), na.strings = c("NA", "-", "?"), 
    colClasses = "numeric")
my <- as.matrix(data[1:57600, 2])
mesh <- array(my, dim = c(120, 60, 8))
Ms <- 1350 * 10^3
asd2 = (mesh[70:75, 24:36, 2])/Ms
ort_my <- mean(asd2)
return(ort_my)
}
<environment: 0x00000000151aef20>

How can I do that?

best regards

Now I used doSNOW package to do same thing

library(foreach)
library(doSNOW)
getDoParWorkers()
getDoParName()
registerDoSNOW(makeCluster(8, type = "SOCK"))
getDoParWorkers()
getDoParName()  

strt<-Sys.time() 

data1 <- list() # creates a list
filelist <- dir(pattern = "*.omf") # creates the list of all the csv files in the     directory
i=1:length(filelist)

readFiles <- function(m){ for (k in 1:length(filelist))
data[[k]] <- read.csv(filelist[k],sep = "",as.is = TRUE, comment.char = "", skip=37);  # to read .omf files skip 37 skips 37 line of the header
my <- as.matrix(data[[k]][1:57600,2]);
mesh <- array(my, dim = c(120,60,8));
Ms<-1350*10^3    # A/m
asd2=(mesh[70:75,24:36 ,2])/Ms;     # in A/m

ort_my<- mean(asd2);
return(ort_my)
}  

out <- foreach(m=1:i, .combine=rbind,.verbose=T) %dopar% readFiles(m)

print(Sys.time()-strt)

I have error messages in following;

Error in readFiles(m) : 
task 1 failed - "object of type 'closure' is not subsettable" 
In addition: Warning message:
In 1:i : numerical expression has 70 elements: only the first used

回答1:


As ?"%dopar%" states, in obj %dopar% ex, ex is an R expression to evaluate. If your free variable in foreach is i, you should use readFiles(i). Currently, you're in fact returning a function object.

BTW, you have some mess in the code. For example, I think that readFiles is independent of x (even if it has x as a formal argument)... Shouldn't it be readLines(filelist[[x]])?



来源:https://stackoverflow.com/questions/23583928/how-to-save-the-output-of-a-foreach-loop-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!