如何在R中保存foreach循环的输出

时间:2014-05-10 16:59:56

标签: r foreach write.table

我在foreach循环

之后保存数据输出时遇到问题

这是读取我的数据并处理它的功能

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

这是进行并行处理的代码

#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')

一切顺利,但当我检查OutputList我只看到function(x)时 我想为文件列表中的每个文件写ort_my

这是我看到的

[[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>

我该怎么做?

最好的问候

现在我使用doSNOW包来做同样的事情

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)

我有以下错误消息;

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 个答案:

答案 0 :(得分:0)

正如?"%dopar%"所述,在obj %dopar% ex中,ex是要评估的R表达式。如果foreach中的自由变量i,则应使用readFiles(i)。目前,您实际上正在返回一个函数对象。

顺便说一下,你的代码有点乱。例如,我认为readFiles独立于x(即使它有x作为正式参数)......不应该是readLines(filelist[[x]])吗?