如何在R中保存一个foreach循环的输出 [英] how to save the output of a foreach loop in R
问题描述
在 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并行运行函数的代码
detectCores()#这会告诉你有多少核心可用
库(foreach);
library(parallel);
library(doParallel)
#library(doMC)这是用于Linux
#registerDoMC(12)#注册并行后端
cl< -makeCluster(4)
registerDoParallel(cl)#为并行后端注册12个cpu
OutputList< - foreach(i = 1:length(filelist),
.combine ='c',.packages = c( data.table))%dopar%(readFiles)
#registerDoSEQ()#非常重要的关闭并行后端。
aa< -OutputList
stopCluster(cl)
print(Sys.time() - strt)
write.table(aa,file =D:/ads.txt ,一切都很顺利,但是当我检查,
> OutputList 我只看到 function(x)
我想写 ort_my $ c $对于filelist中的每个文件来说,都是一样的。
这里是我所看到的
<$ c $
函数(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)
}
;
我该如何做?
最好的问候
现在我用doSNOW包做同样的事情
pre $ $ $ $ $库(foreach)
库(doSNOW)
getDoParWorkers()
getDoParName()
registerDoSNOW(makeCluster(8,type =SOCK))
getDoParWorkers
getDoParName()
strt< -Sys.time()
data1 < - list()#创建一个列表
filelist< - dir(pattern =* .omf)#创建目录中所有csv文件的列表
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); #读取.omf文件跳过37跳过标题
my< - as.matrix(data [[k]] [1:57600,2])的37行;
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)
$ b $ print(Sys.time() - strt)
消息在下面;
readFiles(m)中的错误:
任务1失败 - 类型为'closure'的对象不是子集
另外:警告信息:
在1:i:数字表达式中有70个元素:只有第一个被使用的
ex
是要评估的R表达式。如果 foreach
中的自由变量是 i
,则应该使用 readFiles(I)
。目前,你实际上正在返回一个函数对象。 顺便说一下,代码中有一些乱七八糟的东西。例如,我认为 readFiles
独立于 x
(即使它具有 x
作为一个正式的参数)...不应该是 readLines(filelist [[x]])
?
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
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]])
?
这篇关于如何在R中保存一个foreach循环的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!