![](/img/trans.png)
[英]Is there a way to save partial results of a loop in case of power cut in R?
[英]save partial results in foreach loop in R
我想知道已經完成了多少個循環並保存每個完成的循環的結果,但是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()
這在我的 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.