Is it possible to stop `Rscript` cleaning up its `tempdir`?

给你一囗甜甜゛ 提交于 2019-12-07 03:41:53

问题


I'm using R, via Rscript and H2O, but H2O is crashing. I want to review the logs, but the R tempdir that contains them seem to be removed when the R session ends (i.e. when the Rscript finishes).

Is it possible to tell R/Rscript not to remove the tmp folder it uses?


回答1:


A work around for this would be to use on.exit to get the temporary files and save them in a different directory. An example function would be like this:

    ranfunction <- function(){
#Get list of files in tempdir
on.exit(templist <- list.files(tempdir(), full.names = T,pattern = "^file") )
#create a new directory for files to go on exit
#use add = T to add to the on.exit call
on.exit(dir.create(dir1 <- file.path("G:","testdir")),add = T  )
#for each file in templist assign it to the new directory   
on.exit(
      lapply(templist,function(x){
      file.create(assign(x, tempfile(tmpdir = dir1) ))})
  ,add=T)

}

ranfunction()

One thing this function does not take into account is that if you rerun it - it will throw an error because the new directory dir1 already exits. You would have to delete dir1 before re-running the script.



来源:https://stackoverflow.com/questions/54076077/is-it-possible-to-stop-rscript-cleaning-up-its-tempdir

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