2

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
4

1 回答 1

0

如前所述,?"%dopar%"in是要计算的 R 表达式。如果你的自由变量是,你应该使用. 目前,您实际上是在返回一个函数对象。obj %dopar% exexforeachireadFiles(i)

顺便说一句,您的代码有些混乱。例如,我认为这readFilesx(即使它x作为正式论点)无关......不应该readLines(filelist[[x]])吗?

于 2014-05-10T17:40:18.320 回答