问题
I am trying to change working directory in a future processor, carry out some operations, and exit. The problem is I am not able to set a working directory.
The following toy example works fine
library(future)
dirNames <- as.character(c(1:4))
sapply(dirNames, function(x) if(!dir.exists(x)) dir.create(x))
plan(multiprocess, workers=2)
b <- list()
for(i in seq_along(dirNames)){
sleeptime <- 10
if(i > 3) sleeptime <- 50
a <- future({
# setwd(dirNames[i])
Sys.sleep(sleeptime)
return(2)
})
print(i)
b[[dirNames[i]]] <- a
}
lapply(b, resolved)
lapply(b[1:2], value)
lapply(b, value)
but if I uncomment line 11 then I get following error when running the code
Error in setwd(dirNames[i]) : cannot change working directory
How can I change working directory successfully?
回答1:
I figured out a solution while playing around with the script.
library(future)
dirNames <- as.character(c(1:4))
sapply(dirNames, function(x) if(!dir.exists(x)) dir.create(x))
plan(multiprocess, workers=2)
b <- list()
for(i in seq_along(dirNames)){
sleeptime <- 10
if(i > 3) sleeptime <- 50
a <- future({
currDir <- getwd()
on.exit(setwd(currDir))
setwd(dirNames[i])
Sys.sleep(sleeptime)
return(2)
})
print(i)
b[[dirNames[i]]] <- a
}
lapply(b, resolved)
lapply(b[1:2], value)
lapply(b, value)
I believe that the workers working directory once set in the first few iterations remains permanently set to new directory for remaining iterations and hence future paths (with reference to old directory) do not work.
来源:https://stackoverflow.com/questions/51408736/how-to-change-working-directory-in-asynchronous-futures-in-r