2016-06-19 25 views
2

並列処理部が機能しているが、フェッチ距離でベクトルを保存する処理が正しく行われていない。私が手にエラーが並列処理でR doParallelがデータを保存する

df_Test_Fetch <- data.frame(x_lake_length) 
Error in data.frame(x_lake_length) : object 'x_lake_length' not found 
write.table(df_Test_Fetch,file="C:/tempTest_Fetch.csv",row.names=TRUE,col.names=TRUE, sep=",") 
Error in is.data.frame(x) : object 'df_Test_Fetch' not found 

あるforeachのステップはx_lake_lengthに出力されるように、私は以下のコードを変更しようとしています。しかし、それは私が望むようにベクトルを出力しませんでした。実際の結果をCSVファイルに保存するにはどうすればよいですか。私はRx64 3.3.0でWindows 8コンピュータを走らせています。事前

ジェン

おかげで、あなたはここで完全なコードです。

# make sure there is no prexisting data 
rm(x_lake_length) 

# Libraries --------------------------------------------------------------- 
if (!require("pacman")) install.packages("pacman") 
pacman::p_load(lakemorpho,rgdal,maptools,sp,doParallel,foreach, 
       doParallel) 

# HPC --------------------------------------------------------------------- 
cores_2_use <- detectCores() - 2 
cl   <- makeCluster(cores_2_use, useXDR = F) 
clusterSetRNGStream(cl, 9956) 
registerDoParallel(cl, cores_2_use) 

# Data -------------------------------------------------------------------- 

ogrDrivers() 

dsn <- system.file("vectors", package = "rgdal")[1] 
# the line below is commented out but when I run the script on my data the line below is what I use instead of the one above 
# then making the name changes as needed 
# dsn<-setwd("J:\\Elodea\\ByHUC6\\") 
ogrListLayers(dsn) 
ogrInfo(dsn=dsn, layer="trin_inca_pl03") 
owd <- getwd() 
setwd(dsn) 
ogrInfo(dsn="trin_inca_pl03.shp", layer="trin_inca_pl03") 
setwd(owd) 
x <- readOGR(dsn=dsn, layer="trin_inca_pl03") 
summary(x) 

# Analysis ---------------------------------------------------------------- 
myfun <- function(x,i){tmp<-lakeMorphoClass(x[i,],NULL,NULL,NULL) 
x_lake_length<-vector("numeric",length = nrow(x)) 
x_lake_length[i]<-lakeMaxLength(tmp,200) 
print(i) 
Sys.sleep(0.1)} 

foreach(i = 1:nrow(x),.combine=cbind,.packages=c("lakemorpho","rgdal")) %dopar% (
    myfun(x,i) 
) 
options(digits=10) 
df_Test_Fetch <- data.frame(x_lake_length) 
write.table(df_Test_Fetch,file="C:/temp/Test_Fetch.csv",row.names=TRUE,col.names=TRUE, sep=",") 
print(proc.time()) 

答えて

0

これはあなたが望む通りだと思いますが、主題を理解していないと、私は100%確実ではありません。

foreachを呼び出すと、並列化された関数にreturn()を追加し、返されたオブジェクトの値をx_lake_lengthに割り当てました。しかし、私はそれがあなたがやろうとしていることだけを推測しているので、私が間違っていれば私を修正してください。

# make sure there is no prexisting data 
rm(x_lake_length) 

# Libraries --------------------------------------------------------------- 
if (!require("pacman")) install.packages("pacman") 
pacman::p_load(lakemorpho,rgdal,maptools,sp,doParallel,foreach, 
       doParallel) 

# HPC --------------------------------------------------------------------- 
cores_2_use <- detectCores() - 2 
cl   <- makeCluster(cores_2_use, useXDR = F) 
clusterSetRNGStream(cl, 9956) 
registerDoParallel(cl, cores_2_use) 

# Data -------------------------------------------------------------------- 

ogrDrivers() 

dsn <- system.file("vectors", package = "rgdal")[1] 
# the line below is commented out but when I run the script on my data the line below is what I use instead of the one above 
# then making the name changes as needed 
# dsn<-setwd("J:\\Elodea\\ByHUC6\\") 
ogrListLayers(dsn) 
ogrInfo(dsn=dsn, layer="trin_inca_pl03") 
owd <- getwd() 
setwd(dsn) 
ogrInfo(dsn="trin_inca_pl03.shp", layer="trin_inca_pl03") 
setwd(owd) 
x <- readOGR(dsn=dsn, layer="trin_inca_pl03") 
summary(x) 

# Analysis ---------------------------------------------------------------- 
myfun <- function(x,i){tmp<-lakeMorphoClass(x[i,],NULL,NULL,NULL) 
         x_lake_length<-vector("numeric",length = nrow(x)) 
         x_lake_length[i]<-lakeMaxLength(tmp,200) 
         print(i) 
         Sys.sleep(0.1) 
         return(x_lake_length) 
} 

x_lake_length <- foreach(i = 1:nrow(x),.combine=cbind,.packages=c("lakemorpho","rgdal")) %dopar% (
    myfun(x,i) 
) 

options(digits=10) 
df_Test_Fetch <- data.frame(x_lake_length) 
write.table(df_Test_Fetch,file="C:/temp/Test_Fetch.csv",row.names=TRUE,col.names=TRUE, sep=",") 
print(proc.time())