是否有可能阻止`Rscript`清理它的`tempdir`?

dom*_*mer 6 logging r rscript tmp

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

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

Mik*_*ike 1

解决此问题的方法是使用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()
Run Code Online (Sandbox Code Playgroud)

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

  • 从我的角度来看非常简单:`on.exit(expr = file.copy(tempdir(), log_folder_path, recursive = TRUE))` (2认同)