6

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?

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
dommer
  • 19,610
  • 14
  • 75
  • 137
  • 1
    I think it might be possible to save the temp output - http://rstat.consulting/blog/temporary-dir-and-files-in-r/ maybe pair with `on.exit`? – Mike Jan 07 '19 at 17:12
  • @Mike I was able to use `on.exit` to get round the issue, so, if you want to post that as an answer, I'll accept it. Or I'll add an answer in a few days. Thanks. – dommer Jan 08 '19 at 09:45

1 Answers1

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.

Mike
  • 3,797
  • 1
  • 11
  • 30
  • @dommer I hope this helps/ is similar to what you have. I would be curious to see your solution. – Mike Jan 08 '19 at 15:56
  • 2
    Very simple from my end: `on.exit(expr = file.copy(tempdir(), log_folder_path, recursive = TRUE))` – dommer Jan 09 '19 at 16:03