[英]save partial results in foreach loop in R
I want to know how many loops have been done and save the results of each finished loop, but assign and cat function are not working.我想知道已经完成了多少个循环并保存每个完成的循环的结果,但是assign和cat函数不起作用。
library(doParallel)
library(foreach)
library(Matrix)
rm(list=ls())
cl=makeCluster(2)
registerDoParallel(cl)
sink("report.txt")
result=foreach (n=1:10,.packages="Matrix" )%dopar%{
variable <-sparseMatrix(dims = c(100,150), i={1}, j={1},x=0)
cat(sprintf("tastk %d is complete \n",n),append=TRUE)
assign(paste("variable",n,sep=""),variable)
return(variable)
}
sink()
This works perfectly fine on my Linux computer:这在我的 Linux 计算机上运行良好:
library(doParallel)
library(foreach)
library(Matrix)
rm(list=ls())
cl=makeCluster(2, outfile = "report.txt")
registerDoParallel(cl)
result=foreach (n=1:10,.packages="Matrix" )%dopar%{
variable <-sparseMatrix(dims = c(100,150), i={1}, j={1},x=0)
cat(sprintf("task %d is complete \n",n))
assign(paste("variable",n,sep=""),variable)
return(variable)
}
stopCluster(cl)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.