![](/img/trans.png)
[英]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.