是否可以停止 `Rscript` 清理其 `tempdir`?

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

我通过 Rscript 和 H2O 使用 R,但 H2O 崩溃了。我想查看日志,但包含它们的 R tempdir 似乎在 R 会话结束时(即 Rscript 完成时)被删除。

是否可以告诉 R/Rscript 不要删除它使用的 tmp 文件夹?

解决此问题的方法是使用 on.exit 获取临时文件并将它们保存在不同的目录中。示例函数如下所示:

    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()

这个函数没有考虑到的一件事是,如果您重新运行它 - 它会抛出一个错误,因为新目录 dir1 已经存在。在重新 运行 脚本之前,您必须删除 dir1