How to change working directory in asynchronous futures in R

帅比萌擦擦* 提交于 2021-01-28 06:22:36

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!