[英]R parallel computation with foreach for multiple output by different operation
[英]Different results for same function when parallel R computation
我在Windows 10下使用具有并行库的 system2函数调用外部批处理时遇到问题,我的函数执行一个外部程序以读取二进制文件(必须与示例位于同一文件夹中)。 问题是,当作为简单函数调用时,myfunction返回正确的行数(35行),但并行化时减少了4行(31行)。 在这里您可以找到所有文件的示例 ( https://www.dropbox.com/sh/kdoqdv5uh1rhr98/AAB86TpgVjVlFQRsTOvmZoipa?dl=0 )我的功能如下:
file_to_read<-"crop@seasonal$d.UED"
library(parallel)
cl <- makeCluster(2)
clusterEvalQ(cl, library(base))
seq_along_path_index<-seq_along(all_cells$V1)
list_results<-parLapply(cl,file_to_read,
myfunction)) #return 31
stopCluster(cl)
#simple call
list_results2<-myfunction(file_to_read) #return 35
myfunction<- function(file_to_read) {
setwd('G:/Dropbox/Public/Example')
command<-"./UED_collate.exe"
arg1<-'./crop@seasonal$d.TDF'
crop<- base::system2(command,
args=c(arg1 , file_to_read,
"--captions:", "state", "site", "cycle", "crop"),
stdout = TRUE, wait=TRUE)
n_row<-length(crop)
return(n_row)
}
谢谢
可能来自数据帧也是列表的事实。 例如,当您使用unlist(list(iris, iris))
,将得到大小为1500的数字向量。相反,如果要包含所有数据帧列的列表,请尝试将unlist
与recursive = FALSE
一起使用,或使用do.call("rbind", list(iris, iris))
如果希望数据行附加行do.call("rbind", list(iris, iris))
而不是unlist(list(iris, iris))
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.